Author: serhatsezer

S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles by Robert C. Martin, popularly known as Uncle Bob.

These principles, when combined together, make it easy for a programmer to develop software that are easy to maintain and extend. They also make it easy for developers to avoid code smells, easily refactor code, and are also a part of the agile or adaptive software development.

S.O.L.I.D stands for:

When expanded the acronyms might seem complicated, but they are pretty simple to grasp.

Higher order functions “a function can take either accept functions or closure as a argumant” you’ve already familiar with last one right? You can also write your own higher order function or there are a bunch all ready to take advantage of!

But I want to look at Array high order functions! ( contains, drop, first, flatMap, forEach, partition and split, – yaaaaay )

I just want to you to just realize you can’t remember how to use it and resort reinventing the wheel. Let’s take a look at some of these more obscure higher order functions, and compare how some would need to be coded in for-in or while loop.

Sorted
Create a sorted version of an array. You don’t need to see an example of manually sorting an array to understand how this works!
Sort an array simply by describing how you want the sort to work. You can do this simply by indicating the direction of the sort with a less than mark or greater than mark.

First
You’re probably familiar with the first property that retrieves the first element of an array, but did you know you can pass in a condition to get only the first element that meets that condition?

eg. Let’s say we want the first element of the array that contains the letter ‘i’.
Instead of:

Notice that this method, rather than returning a new array, actually changes the array itself. Notice also that the order of the elements within each partition changes in a somewhat random fashion.

Split
You may be familiar with the components method on String, used to split a String based on a separator.

eg. Let’s say we have a paragraph that we want to split into sentences. We could use the components method, checking for full-stops (aka periods):

let paragraph = "I can't believe it! These higher order functions are like magic. Don't you think? Well, maybe not magic, but pretty useful all the same."
let sentences = paragraph.components(separatedBy: ".")
//["I can\'t believe it! These higher order functions are like magic",
//" Don\'t you think? Well, maybe not magic, but pretty useful all the same",
//""]

What’s with that final element though? An alternative to the components method is the split method, which accepts a separator too, but by default will omit blank elements.

To use the split method on a String, you would use it on the String.characters property, which is a String.CharacterType, which adopts the Collection protocol, giving characters access to many of the same cool higher order functions that Array has access to. Once you’ve separated String characters with split, you’ll have an array of something called a SubSequence, that you can pass in when initializing a String – you can do this on each element of your new array using the map higher order function to end up with an array of Strings.

Well that’s it! I hope next time you need to perform some magic on an array, you too might remember and take advantage of one of these higher order functions, keep your code nice, pretty and succinct and remember that you don’t need to reinvent the wheel!

A common Swift problem is that extensions of generics can only be constrained to protocols. We use a lot of big words here but this issue is quite common and a simple example will clearly illustrate this situation.
So let’s assume that we want to detect if an optional string is either empty or nil.
The basic solution would look something like this

let myString: String? = …
if myString?.isEmpty ?? true { … }

This looks like a bit bloated for such a simple operation and we would rather use something like

let myString: String? = …
if myString.isEmpty { … }

However this property does not exist and we are immediately thinking of adding an extension to Optional type to help us doing this :

Swift only accepts protocol as constraints on generic type extensions.
This especially does not work on value types (structs, enums, base types).
An interesting thing is that swift implicitely provides a protocol for classes describing their interface. You can therefore constraint to classes easily.
We will now see how to constraint on value types.

Solution

protocol PossiblyEmpty {
var isEmpty: Bool { get }
}

Now let’s retrofit it on String. We only need to declare it as the implementation already exists :

extension String: PossiblyEmpty {}

Now let’s update our Optional extension to support this protocol:

extension Optional where Wrapped: PossiblyEmpty { … }

and we are done:

let myString: String? = …
if myString.isEmpty { … }

Bonus code: we can even retrofit more implementations as long as they provide an implementation for this signature !

extension Array: PossiblyEmpty {}

Here is the full code, you can add comment, edit or anything you want!

Also be sure to remove Main.storyboard declaration in the “Deployment Info > Main Interface” section. After you do this go to your storyboard and separate session logic like adding two separate navigation controller and identifier them accordingly

Auto Layout works great in Interface Builder, but it’s often helpful to have the flexibility and clarity of wiring up constraints in code. Let’s dive in.

We’ll add a view and set translatesAutoresizingMaskIntoConstraints to false. Normally Interface Builder does this automatically under the hood, but since we’re working in code we’ll need to set it ourselves. Don’t want any funky autoresizing constraints in there meddling around.

Whew! That’s a long constructor. The neat part though, is from left-to-right it almost reads like the equations from before. Lastly, we’ll assign a fixed size to our logo view. Since there’s only 1view involved, the equation is much simpler:

Objective-C ensure you more possibilities when you’re manipulating existing objects like NSArray, NSString, NSDictionary etc. Some API functions can be done less code and some other snippets really mess.

Before I learn reverse string block syntax I was doing this iterate trough all exist string length and after the range this string I was add these reversed string in a mutable array. Here is the my old approach

Sometimes we need to scale to fill our images to fit in certain image view. This code snippet easly do this. I definitely recommend to you use UIImage category for this. So that you don’t repeat same code again and again.

When we working on a project and it has lot of class where doing same job we don’t need to import these classes again and again. Instead we need to a model class. Declare these classes in a single model class and use this class when necessary.

In Objective-C some APIs may wish to expose own helper functions publicly. For concern and state procedures. Functions are great way to encapsulate like these behaviours. If you think they’re really useful, it may be worth making them available globally.