Filters

Hide filters
Platform
Content Type
Difficulty

All Tutorials · 31 Results

Contained in: UIKit Apprentice strings
iOS & Swift

Chapter in UIKit Apprentice

Networking

…take these step-by-step. Create the URL for the request ➤ Add a new method to SearchViewController.swift: // MARK: - Helper Methods func iTunesURL(searchText: String) -> URL { let urlString = String( format: "https://itunes.apple.com/search?term=%@", searchText) let url = URL(string: urlString) return url! } This first builds a URL string by placing…
iOS & Swift

Chapter in UIKit Apprentice

Internationalization

…confirmation because this involves moving the file to a new folder: Xcode asks whether it’s OK to move the file ➤ Uncheck Use String Catalog and then choose English (not Base) and click Localize to continue. Look in Finder and you will see there is a new en.lproj — for English…
iOS & Swift

Chapter in UIKit Apprentice

Polishing the App

…gets the biggest makeover, but you’ll also tweak the others a little. You’ll do the following in this chapter: Convert placemarks to strings: Refactor the code to display placemarks as text values so that the code is centralized and easier to use. Back to black: Change the appearance…
iOS & Swift

Chapter in UIKit Apprentice

Swift Review

…types Variables and types A variable is a temporary container for a specific type of value: var count: Int var shouldRemind: Bool var text: String var list: [ChecklistItem] The data type, or just type, of a variable determines what kind of values it can contain. Some variables hold simple values…
iOS & Swift

Chapter in UIKit Apprentice

Slider & Labels

…bunch of other messages in the Console, too. This is debug output from UIKit and iOS Simulator. You can safely ignore these messages. Strings To put text in your app, you use something called a “string.” The strings you have used so far are: "Hello, World" "This is my first…
iOS & Swift

Chapter in UIKit Apprentice

Get Location Data

…print() you added earlier in there because it’s handy for debugging. ➤ Add the updateLabels() method: func updateLabels() { if let location = location { latitudeLabel.text = String( format: "%.8f", location.coordinate.latitude) longitudeLabel.text = String( format: "%.8f", location.coordinate.longitude) tagButton.isHidden = false messageLabel.text = "" } else { latitudeLabel.text = "" longitudeLabel.text = "" addressLabel.text = "" tagButton.isHidden = true messageLabel.text = "Tap 'Get My Location' to Start" } } Because…
iOS & Swift

Chapter in UIKit Apprentice

Add Item Screen

…override func tableView( _ tableView: UITableView, numberOfRowsInSection section: Int ) -> Int { return "1" } Then, the compiler would give an error message, as "1" is a string, not an Int. To a human reader they look similar and you can easily understand the intent, but Swift isn’t that tolerant. Data types…
iOS & Swift

Chapter in UIKit Apprentice

Outlets

…replacing the existing code with actual ellipsis! :] You’ve simply added the random number, which is now stored in targetValue, to the message string. This should look familiar to you by now: The \(targetValue) placeholder is replaced by the actual random number. The \n character sequence is new. It means…
iOS & Swift

Chapter in UIKit Apprentice

Search Bar

…moment, all the new code does is to output the search term from the search bar to the Xcode Console. Tip: I always put strings between single quotes when I use print(). That way you can easily see whether there are any trailing or leading spaces in the string. Also…
iOS & Swift

Chapter in UIKit Apprentice

Refactoring

…code to it: import Foundation class Search { var searchResults: [SearchResult] = [] var hasSearched = false var isLoading = false private var dataTask: URLSessionDataTask? func performSearch(for text: String, category: Int) { print("Searching...") } } You’ve given this class three internal properties, one private property, and a method. This stuff should look familiar because…
iOS & Swift

Chapter in UIKit Apprentice

The Tag Location Screen

…good place to display the passed in values on screen. ➤ Add the following code to LocationDetailsViewController.swift: override func viewDidLoad() { super.viewDidLoad() descriptionTextView.text = "" categoryLabel.text = "" latitudeLabel.text = String( format: "%.8f", coordinate.latitude) longitudeLabel.text = String( format: "%.8f", coordinate.longitude) if let placemark = placemark { addressLabel.text = string(from: placemark) } else { addressLabel.text = "No Address Found" } dateLabel.text = format(date: Date()) } This…
iOS & Swift

Chapter in UIKit Apprentice

URLSession

…your error handling code. So, let’s first fake an error and get that out of the way. ➤ In iTunesURL(searchText:), change the URL string to: "https://itunes.apple.com/NOsearch?term=%@&limit=200" Here, I’ve changed the endpoint from search to NOsearch. It doesn’t really matter what you type there, as long…
iOS & Swift

Chapter in UIKit Apprentice

Saving Locations

…your own methods to it. ➤ Add the rest of the attributes for the Location entity: longitude – Type: Double date – Type: Date locationDescription – Type: String category – Type: String placemark – Type: Transformable The data model should look like this when you’re done: All the attributes of the Location entity Why didn…
iOS & Swift

Chapter in UIKit Apprentice

UI Improvements

…tableView(_:cellForRowA:t): cell.detailTextLabel!.text = "\(checklist.countUncheckedItems()) Remaining" You call the countUncheckedItems() method on the Checklist object and put the count into a new string that you display using the detailTextLabel. As usual, you use \(…) to do the string interpolation. Notice that you can even call methods inside interpolated strings. Sweet…
iOS & Swift

Chapter in UIKit Apprentice

Use Location Data

…updateLabels() { if let location = location { . . . } else { . . . // Remove the following line messageLabel.text = "Tap 'Get My Location' to Start" // The new code starts here: let statusMessage: String if let error = lastLocationError as NSError? { if error.domain == kCLErrorDomain && error.code == CLError.denied.rawValue { statusMessage = "Location Services Disabled" } else { statusMessage = "Error Getting Location" } } else if !CLLocationManager.locationServicesEnabled…
iOS & Swift

Chapter in UIKit Apprentice

Rounds & Score

…where in the code can you do that? In updateLabels(), of course. ➤ Back in ViewController.swift, change updateLabels() to the following: func updateLabels() { targetLabel.text = String(targetValue) scoreLabel.text = String(score) // add this line } Nothing new here. You convert the score — which is an Int — into a String and then pass that string…
iOS & Swift

Chapter in UIKit Apprentice

Local Notifications

…approach. Instead, you’ll use an identifier. When you create a local notification, you need to give it an identifier, which is just a String. It doesn’t really matter what is in this string, as long as it is unique for each notification. To cancel a notification…
iOS & Swift

Chapter in UIKit Apprentice

Lists

…parameter. Then you can simply write: list = Checklist(name: "Name of the checklist") ➤ Go to Checklist.swift and add the new init method: init(name: String) { self.name = name super.init() } This initializer takes one parameter, name, and places it into the property called name. Notice that while the parameter and property…
iOS & Swift

Chapter in UIKit Apprentice

Polish

…existing method with it: @IBAction func showAlert() { let difference = abs(targetValue - currentValue) let points = 100 - difference score += points // add these lines let title: String if difference == 0 { title = "Perfect!" } else if difference < 5 { title = "You almost had it!" } else if difference < 10 { title = "Pretty good!" } else { title…
iOS & Swift

Chapter in UIKit Apprentice

The Detail Pop-up

…correct currency. ➤ Add the following code to the end of updateUI() in DetailViewController.swift: // Show price let formatter = NumberFormatter() formatter.numberStyle = .currency formatter.currencyCode = searchResult.currency let priceText: String if searchResult.price == 0 { priceText = "Free" } else if let text = formatter.string( from: searchResult.price as NSNumber) { priceText = text } else { priceText = "" } priceButton.setTitle(priceText, for: .normal) You’ve used…