Skip to content

Instantly share code, notes, and snippets.

@theevo
Created March 26, 2020 18:12
Show Gist options
  • Save theevo/c435df8b65821b441e341e05a7d98bac to your computer and use it in GitHub Desktop.
Save theevo/c435df8b65821b441e341e05a7d98bac to your computer and use it in GitHub Desktop.

Obj-C FAQ with Jared

Instructor: Jared Warren

Date: 2020-03-26

What is the difference between Atomic and Non-Atomic? Copy, Nonatomic etc.. readonly, atomic, readwrite,

Best resource: https://academy.realm.io/posts/tmi-objective-c-property-attributes/

atomic = atom from science means uncuttable. Cannot be accessible by multiple threads. nonatomic = you can access it from multiple threads.

Why would you want to copy a string?

textField.text

myPerson.name

Imagine if both point to the same place in memory. If you change textField.text, you will change myPerson.name. You just violated MVC. These two things should be distinct copies.

It's important to choose the right type

bits values int float
8-bit 256 values Int8
16-bit 65,535 values Int16
32-bit 4,294,967,296 values Int32 Float
64-bit 1.8446744e+19 values Int64 Double

Swift will choose 32 or 64-bit based on the target iPhone if you declare just Int.

iPhone 5s and newer are 64-bit. Older iPhones are 32-bit.

Is there anything Objective-C does better than Swift? Or vice-versa? (Is there ever an advantage to using Obj-C over Swift, other than the fact that a given project is already using Obj-C?)

Objective-C has faster compile time and build time. Run time might be slightly faster in Swift. Snapchat chose to stay in Objective-C because it loads the camera faster.

Cell(protocol) tells TableView(delegate), "Hey my switch got tapped." This is a 1 to 1 communication pattern.

There's a 1 to many. Like Notifications. Example: Users taps button for Spanish in Main ViewController, notifications will tell all other VCs that language is updated.

What advantages are there for writing your Models and Model Controllers in Obj-C over Swift (if any)?

Key Value Observation. It's kind of like didSet. If you have an NSObject, you have access to this throughout your app. It's 1 to 1 but slightly hybrid in that it can be extended to 1 to many.

Proper use cases for keywords: static, const, alloc, “PRETTY FUNCTION”?

static - in Obj-C, this variable will be available for the lifetime of the app const - the pointer is constant. Neither pointer nor value can be changed. just like let in Swift. Ex: baseURL PRETTY FUNCTION - helps with debugging. Print it out to determine which function threw the error. Just like #function in Swift.

@interface vs @class

@interface - defines a class @class - "trust me this cell exists". It's not a full #import.

@class WRRCell;

@protocol WRRCellDelegate
@end

@interface WRRCell
@end

What is a closure? And when are they implemented in code?

Warning: it's closure in Swift; block in Objective-C.

An Objective-C class defines an object that combines data with related behavior. Sometimes, it makes sense just to represent a single task or unit of behavior, rather than a collection of methods.

Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary. They also have the ability to capture values from the enclosing scope, making them similar to closures or lambdas in other programming languages.

Source: Working with Blocks

What's the difference between closure and block? For now, just accept that they're similar. Later, you'll understand their nuances.

Where to find good docs for Obj-C as Apple Docs default to Swift

Frameworks documentation can be switched between Swift and Obj-C. Top right hand corner, there's a Language drop down. EX: NSURLSession and URLSession.

To learn the language of Objective-C, see official documentation:

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html

Swift language documentation:

https://swift.org/getting-started/#installing-swift

When are <> typically used?

In Obj-C, to conform to protocols. You want to specify the contents of an array like NSArray<int>.

When/how to use alloc

Please explain the difference between [[myObject alloc] init] vs [myObject new]. Why even bother with alloc?

alloc - allocates memory. That's it. Just allocates memory for this object.

What if you wanted to use a different initializer?

[myClass new];
[[myClass alloc] initWithString:someString];

What exactly is a pointer and is there a correlating equivalent in Swift?

First, we need background.

What is RAM? It's working memory. It's the memory that gets reset every time the device is restarted. We use it to store everything we're doing. We're constantly reading and writing to it.

There are 3 places in memory

  1. Stack
  2. Heap
  3. Static memory (for your static variables)

How stack works - FILO

  • multiply()
  • gameOfThrees()
  • main()

Your functions get placed sequentially on the stack first in, last out. First main, When each a function completes, it is popped of the stack so that the function that called it can continue. This repeats

Ex: navigation controller stack.

Stack Overflow - when you have an infinite loop.

Stack's size is constant. 1MB. It won't grow. You'd be hard pressed to fill the stack legitimately. It's plenty big for what we need it for.

What if we want an image? The stack cannot fit a 4MB selfie. We put the image on the heap. Then the stack contains a pointer to that image.

Heap can grow as needed.

Arrays are actually pointers to the actual arrays on the heap. Array elements are usually next to each other on the heap.

Values are unique copies. References are pointers that point to the same address on the heap.

Is there a limit for the heap? Hardware will limit.

This concept applies to all computers and programming languages. Swift is subject to these memory rules too.

How much should we be stressing about retain cycles and memory leaks?

Memory leaks are bad, and we don't want to have a slow or unreliable app. We should be aware of this for now. Be conscious of it, but don't stress over it. Your main goal at this point in your career is just get your app to work.

As soon as pointers die on the stack, ARC in Swift takes care of removing objects from the Heap.

// TableViewController

cellForRow {
cell.delegate = self
}


// cell

var delegate: ?

These two objects reference each other. This is called retain cycle. You have a memory leak. You're using way too much memory.

// cell

weak var delegate: ?

weak reduces the reference count, so when the cell pointer is gone, the cell is deallocated from memory.

Balloon analogy. A string is like a strong ref. Eyes looking at the balloon are like weak references. Strong references are the only thing keeping the balloon grounded. It doesn't matter how many weak references are looking that balloon, if we lose enough strong references, balloon floats away / dies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment