Swift
Swift Everyday Ep #2
Ditch rawValue use callAsFunction on enum
Swift Everyday: Ep #1
How to add AppDelegate in SwiftUI App
Swift Tips: Switch
Please find Swift playground at github.com
This post shares from simple to advance the use of switch cases
in swift. These can be enhanced and some time shorthand of more lines of code. I hope you would enjoy them and try to use in your coding style. Please let me know your feedback in the comments if you have any. Happy coding.
How to check whether a module is available using canImport()
Writing multi-platform code has its own challenges, but if you use the canImport()
compiler test then one big challenge is solved for you: you can write one chunk code to run if a specific module is available, and another chunk otherwise.
How to enumerate items in an array
There several ways to loop through an array in Swift, but using the enumerated() method is one of my favorites because it iterates over each of the items while also telling you the items’ position in the array.
Here’s an example:
[swift]
//How to enumarate a array
let array = [“Hulk”, “Spidy”, “Thor”]
for (index, item) in array.enumerated() {
print(“Found \(item) at position \(index)”)
}
[/swift]
That will print “Found Hulk at position 0”, “Found Spidy at position 1”, and “Found Thor at position 2”.
|
Simple trick to put end to those empty tableview cells
Just like me, I am sure you all have been annoyed by those empty table view cell after your data is loaded.
@IBOutlet internal var tableView: UITableView! { didSet { tableView.tableFooterView = UIView() } }
|
Always Try, Try?, And Try!, Never give up
Beside Swift being powerful, expressive language with a flexible, elegant syntax. There is one of the features I appreciate most is native support for error handling with try keyword.
It is easy to ignore errors in Objective-C. You have to do a bit of extra work to enable error handling.
[swift]
NSError *error = nil;// Initialize Data With Contents of URL
NSData *data = [[NSData alloc] initWithContentsOfURL:URL options:0 error:&error];
if (error) {
// Error Handling …
}
[/swift]
|
Understanding more about MVVM
We all know about MVC and it’s shortcomings. I would love to explain MVVM that make developer’s life as well as testing quite easy
Creating a View Model
Before we take a look at View Model. let’s tale a look at something familiar. You would feel home if you’ve been using the Model-View-Controller pattern to build Swift applications. The moment the view controller has finished loading its view, it performs a network request. It delegates this task to the APIManager
class, Continue reading
|
Pure Swift Protocol ‘weak var delegate’ error
Just like anyone else you have encounter this error too. Writing a pure swift protocol and declaring the weak delegate you must have encountered error mentioned below.
[swift] weak var delegate: TestProtocol? [/swift]
|