Our iOS experts are here to answer the top 10 questions about iOS development on the internet. The post was contributed to us by Artur Rożek, Senior iOS Developer, with contributions from Stanisław Myszkowski, Senior iOS Developer, and additional help from Michał Skrzypek, Senior iOS Developer.
1. How Is Swift Better Than Objective-C?
Swift is a modern language created in 2014, while Objective-C is much more like C/C++ and was created in 1984. Objective-C lacks many features which are in Swift, such as optional types, modern structs and enums with associated values, tuples and even in the newest version (>=5.5) async features build right into the language syntax (see async/await docs here: https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html)
2. What Is SwiftUI?
SwiftUI is a framework to use with Swift language to build user interfaces in the declarative manner and very fast as well. SwiftUI’s look and behavior depends on some state (or a set of states).
When using said framework, it is ideal to use the MVVM (Model-View-ViewModel) pattern where the ViewModel is an observable object, and the View just observes the ViewModel and changes its look depending on what is going on in the ViewModel. However, under the hood it is mostly the UIKit.
3. Is SwiftUI Production Ready?
If your project supports iOS 15 and newer versions, then the answer is yes. There are plenty of new features which helps develop rock solid applications. However, if your application must support iOS 14 or even iOS 13 then we would not recommend SwiftUI. Prior to iOS 15 there are plenty of SwiftUI features which are missing (modifiers such as .refreshable, .searchable, .badge, .interectiveDismissDisabled, .toolbar and so on, not to mention @FocusState, AsyncImage or SecureField).
There are 3rd party libraries that “hack” SwiftUI views by modifying the underlying UIKit components, such as https://github.com/siteline/SwiftUI-Introspect
4. How Can I Check for an Active Internet Connection on iOS or macOS?
The most convenient way is to use an external library. One of our favorite ones is https://github.com/ashleymills/Reachability.swift which gives you the possibility to observe network status when it changes from online to offline and vice versa.
5. How to Download/Load an Image From URL?
You can load data from an URL using URLRequest/URLSession and then convert it to an image, but the better way is to use an external library which loads images asynchronously, such as KingFisher available under the link https://github.com/onevcat/Kingfisher or, if you support iOS 15 and above, you may use AsyncImage: https://developer.apple.com/documentation/SwiftUI/AsyncImage
6. How Can I Detect Any Text Changes in a UITextField?
The best way to do it is to assign a delegate to an UITextField instance (see delegate documentation here: https://developer.apple.com/documentation/uikit/uitextfielddelegate). Afterwards, you may use textField(_:shouldChangeCharactersIn:replacementString:) delegate method to detect text changes or listen to textDidChangeNotification notification (I would recommend the first method).
7. How Do I Programmatically Create Graphical Elements (like a UIButton) in Swift?
Like every other object in Swift. However, if you’d like such a view to be visible to the user you have to
- Add it to the parent view
- Establish some constraints to that view within the parent view
Below is the code to create and make a button fill the parent view:
8. How to Convert an UIImage Into a Base64 String?
Assuming you have an image variable of type UIImage, then
Let value will be optional, so you may use guard to unwrap it. Please remember that the base64 string will contain PNG data. If you want to have a JPEG instead (with defined picture quality), then use:
9. How to Declare an Empty Dictionary in Swift?
For dictionary with keys and values of type String:
Alternatively, you may use:
We would NOT recommend the syntax below:
10. How to Update to the Latest Version of CocoaPods
The simple way (although not the best)
Use an existing ruby installation shipped with your Mac. You will need to run the gem command with admin privileges, like so:
This approach will work if you’re working on a small project, and you don’t need a CI system.
The "not-so-simple" way
A better approach is to use a standalone ruby installation using either rbenv or rvm, so it doesn’t modify system ruby. Let’s focus on rbenv. For that, you will need to install Homebrew first (if you haven’t already), and then install rbenv:
When this step is complete, put the following in your ~/.zprofile (or ~/.bash_profile, if you’re using bash)
You can then proceed with
The Bundler way
This one comes in handy when you’re working on a larger project and want to make sure all ruby versions are consistent across dev machines and the CI system. It’s also a good idea if you have more ruby tools in your pipeline (such as fastlane, for instance)
Basic steps include setting up a separate ruby installation, like in the previous section. This time, we’re also going to create a `Gemfile` file that specifies all the gem versions.
This file could look like this:
...the bundler will download all dependencies, just as you would be using plain gem install.
The advantage is that you only need to issue a single command.
In order to update a gem (for example, cocoapods), type
Then, if you want to run any of the bundler gems, you need to prefix the command with bundle exec, for example:
Just like installing it for the first time. Go to the terminal and just type:
That requires Ruby to be present on a Mac, though you don’t have to install Ruby separately. Ruby is already preinstalled on your Mac, though remember to use sudo.