Creating and Distributing iOS Frameworks | Swift | Tips & Tricks

Why do we need to create frameworks? because it has three major purposes mentioned below and also you can share your code with your team, other developer or iOS community.

  • Code encapsulation
  • Code modularity
  • Code reuse

Let’s create the iOS framework.

Screen Shot 2018-02-05 at 8.24.50 PM

Now just give some meta-data about your framework like name, organization, identifier etc (I hope you’re already aware of it).

Screen Shot 2018-02-05 at 8.26.54 PM.png

We’re good to go till now. Just add the files that you want to keep within your framework.

add

Build the framework project to make sure that you get build succeeded with no build warnings or errors.

Things that you need to take care.

  • Make sure to check Copy items if needed, so that the files actually copy into the new project instead of just adding a reference. Frameworks need their own code, not references, to be independent.
  • Double-check that each of the files has Target Membership in ThreeRingControl to make sure they appear in the final framework. You can see this in the File Inspector for each file.
  • Double-check the access modifiers, while creating framework access modifiers plays very important roles.
  • if you’re creating Swift framework make sure you’re extending classes from NSObject otherwise it won’t reflect once you will import the framework.

Thanks for reading.

Clean Code | TDD | Swift | iOS | Quick Notes

TDD is a way of writing software, where we write as little code as possible to make the test pass and then late refactor the code.

Golden laws from Uncle Bob

  • You may not write production code until you have written a failing unit test.
  • You may not write more of a unit test that is sufficient to fail and not comping is failing.
  • You may not write more production code that is sufficient to pass the currently failing test.

Whenever we create a new iOS app, we start with UI/ViewController/Business Login directly but Idea here is to start with the Test-cases. How? Let’s say you want to add the functionality to find out the divisible or not as below mentioned the code. Write down the test-case first.

func testIsDivisibleByThree() {
let brain = Brain()  
let result = brain.isDivisibleByFive(number: 5)
XCTAssertEqual(result, true)
}

But we don’t have the class Brain till now!!! So create a class adds the function, don’t forget the aim is to write as minimum code as you can.

class Brain {
    func isDivisibleByThree(number: Int) -> Bool {
        return true
    }
}

Because as of now, we are returning static boolean value, test-case will be a pass for sure. Same pattern we need to follow for each and every code we are going to write.

Simple words I can say there are 4 stages in TDD.

RED – Write down the test case
GREEN – Write just enough code to make the test-case pass
REFACTOR – Clean up existing code
REPEAT – repeat the cycle

In above codebase we have done RED and GREEN, let consider the stage 3rd, REFACTOR.

Let’s say now we have to write a function where we need to check whether a number is divisible by given number or not, like this

func isDivisibleBy(divisor: Int, number: Int) -> Bool {
  return number % divisor == 0
}

So we have refactored the code, Utilizing it like.

func isDivisibleByThree(number: Int) -> Bool {
    return isDivisibleBy(divisor:3, number:number)
 }

the idea is to repeat these steps so we can make sure when we are making any new changes, is it breaking some existing functionality.

I have tried to create short notes on how to follow TDD but also Idea is here is to decouple your code from UIViewController as much as possible so that it will give you more testability scope (for implementing this I have tried MVVM design pattern in few my recent projects, will share one quick notes on that as well).

Thanks for reading.

STUPID principles | Quick Notes

Recent blog: SOLID vs STUPID

STUPID principles and yes this may hurt your feeling but yes if you are following this you are writing stupid code.

Singleton

Well known design pattern, but mostly understood one as well. Like me, people are also fighting with Singleton syndrome. In that case, whenever we create a class we create it Singleton but why? That is definability not cool. Singleton also considered anti-patterns. We should think and avoid if possible because the program using Singleton are hard to test because of global state, with the global state they hide their dependencies.

Tight Coupling

Avoiding static things is important to avoid tight coupling. If it’s there then making a change in one module requires to changes another module too. Now it’s really difficult to reuse and difficult to test.

Untestability

Testing is not hard, what you think? We don’t write test case may be because we don’t have the time or the code we are writing is bad because of tight coupling?

Premature Optimization

One of my favorite quote from Donald Knuth, premature is the root of all evil. There are only cost and no benefits. Actually optimised code is much complex as compared to just writing a loop.

Indescriptive Naming

That’s what I like most and I try to follow as much as possible. Name of your class, variables or methods should be clear because at the end of the day human is only going to read because programming languages are for human not for computers, computers can understand 0 or 1 very well.

Duplication

That’s bad. Writing same code again and again so don’t repeat yourself, keep it simple and strong. Write Code only once.

Thank you for reading the quick introduction of STUPID principles, next will be SOLID principles.

SOLID principle with Swift | STUPID

SOLID principle is an acronym created by Robert C Martin also unknown as Uncle Bob. It represents five principles for OOPS.

Single responsibility
Open/Closed
Liskov Substitution
Interface Segregation
Dependency Inversion

Now the first thought that came to my mind is Why do we need this? Here is the answer, using these principles we can solve the problems of a bad architecture.

Fragility where A small change may break complete module it’s really very difficult to find this if you don’t have good test cases.

Immobility where A component is very hard to reuse in another project or we can say multiple places in the same project because of too many dependencies.

Rigidity where Single change requires lots of developer efforts because it affects several parts of the project.

Here I want to add principles will not turn a bad programmer into a good programmer you need a better judgment there. Principles have to apply with judgment and you must be smart enough to understand when to apply what.

I have also written one blog where they have mentioned one more acronym like SOLID and it’s STUPID. This may hurt your feeling but yes if you are following this you are writing stupid code.

Singleton
Tight Coupling
Untestability
Premature Optimization
Indescriptive Naming
Duplication

Thank you for reading the quick introduction, In next blogs, I will try to define all principles with Swift Code.

Signs of wrong architecture? | Quick Notes

Recently I have got one iOS Application codebase to review and it always gives a thought, where to start?

Here are the 3 important first steps for me that I always do.

find . -type f -exec wc -l {} + | sort -n

Execute the terminal command in your project location, it will give you count of number of lines, like mentioned below

3000 ./ViewControllers/DashboardVC.swift
5655 ./AppDelegate.swift

where 3000/5655 is the lines of code.

Second, I track are we using Appdelete directly?

grep -rnw . -e "UIApplication.shared"

Last, but not the least. Swift/Objective-C dependency visualizer. It creates object dependency graphs and gives you the clear picture.

Screen Shot 2017-11-22 at 5.33.12 PM

Thanks for reading. I hope it was quick and informative for you.

Safety In Swift | Fun Time

Let’s say we have a Car and we want it to be safe so we have seatbelts and airbags and we don’t want to let the truck drive unless safety points passed.

Code Sample

enum Safety {
    case seat
    case door
    case airbag
}

struct Car {
    var seatBeltEnabled: Bool
    var doorLocked: Bool
    var airBagsWorking: Bool
}

extension Car   {
    func drive() throws {
        guard seatBeltEnabled else {
            throw Safety.seat
        }

        guard doorLocked else {
            throw Safety.door
        }

        guard airBagsWorking else {
            throw Safety.airbag
        }
        //Everything fine.... drive carefully
    }
}

I hope everything is looking fine so that’s the beauty of Swift otherwise in other approaches we need to manage it with if-else before we start driving.
That’s all about the small beautiful thing I learned today.

App Secure | URLSession | Authentication Challenge | NTLM | Security | Credentails

Yesterday, I have posted about How to response Authentication Challange but thoughts came in mind that if you are going with the first options Provide authentication credentials is it really secure and safe? how is client sharing the credentials with the server?

After lots of Google, I have found, how’s NTLM works and it’s pretty interesting to see that client don’t share the password with the server. here are the steps as follow.

Screen Shot 2017-10-12 at 1.06.51 PM.png

  1. The client makes the request to the server.
  2. The server needs to validate the user because there is no identity so server generates 16 bytes random number called as the challenge and sends it to the client.
  3. Client hash this challenge with the user’s password and return it back to the server that is called the response it also includes username as plain text and challenge sent to the client.
  4. The server sends everything to the domain controller and it uses the username to retrieve the hash of the user’s password from security account manager database and hash the challenge.
  5. Domain controller shares the response back to the server if they are identical then authentication is successful otherwise a failure.

So the interesting part is here that Network API doesn’t share the password with the server it means it very secure.

Thank you for reading.

Share your thoughts and feeback.