Skip to content
Sergey Lukin edited this page Feb 7, 2016 · 19 revisions

Shortcuts

  • Cmd + 0 - hide/show navigation panel (left)
  • Fn + Cmd + 0 - hide/show attributes panel (right)
  • Cmd + r - build and run simulator
  • Cmd + Enter - switch to Standard editor. Useful when switching from Assistance editor
  • Fn + Cmd + Up/Down - switch between *.h / *.m related files
  • Fn + Cmd + Left/Right - go back and forth between code reference jumps (which is done by holding Cmd and left clicking on a reference in the code)
  • Shift + Cmd + o - search for header files
  • Option + Drag element in Storyboard - create fresh copy of element
  • Fn + drag element from Storyboard to ViewController - assign connection between gui element and Controller method

Simulator

Change simulator scale with following shortcuts:

  • 100% scale use CTRL+1 or CMD+1
  • 75% scale use CTRL+2 or CMD+2
  • 50% scale use CTRL+3 or CMD+3
  • 33% scale use CTRL+4 or CMD+4
  • 25% scale use CTRL+5 or CMD+5

Objective-C stuff I wouldn't remember

Methods call

Square brackets send messages (call methods?). In square brackets there may be 2 entities separated by a space. Basically, the left one is a to or the Object or the Class, the right one is the message or the method to call. Square brackets "calls" can be nested.

Some examples:

NSString myString = @"foo bar";
double myDouble = [myString doubleValue]; // or double myDouble = myString.doubleValue;

That's simple one. myString is a variable that has a string value, so in order to convert it to a double we call getter method of doubleValue property on Object myString;

Now let's take a look at a horribly complicated one:

[self.calculator addNumber:[NSNumber numberWithDouble:myDouble]];

What the heck is going here the first time I said to myself when saw this syntax. Well, will try to explain to myself now. We have to calls here, one is calling static method numberWithDouble with parameter myDouble on NSNumber class and another one is passing the resulting value of first call as a parameter to call method addNumber on some object that was instantiated in instance property calculator. Huh, I hope I got it at least 50% right. Anyways, here is how that would look like in JS with same objects/variables/methods/parameters names:

this.calculator.addNumber(NSNumber.numberWithDouble(myDouble))

I haven't seen much of Objective-C code yet but I hope nesting calls like that is not a default practice among Objective-C community and breaking such inlines into multiple lines is preferred.

Interesting findings.

These are equal:

self.value = [NSNumber numberWithDouble:result];
[self setValue:[NSNumber numberWithDouble:result]];

however this one is not correct:

self.value = NSNumber.numberWithDouble:result;

as . is used to call getters or setters of properties but not to call methods.

These are identical:

double myVal = self.value.doubleValue;
double myVal = [self.value doubleValue];

Did you notice that square brackets are more universal than . access? It can be used to call both properties getters / setters and other methods.

Arrays

You can fetch value by index from an array with one of following methods:

id favorite = [favorites objectAtIndex:1];
id favorite = favorites[1];

Is there anyone who might prefer the first one? I don't think so, but knowing what first one does actually helps to understand how the language works.

id

id is a generic pointer type that can be a pointer to any object. In pure English? No strong typing. Assign any object to a variable. Example: id myObj = [[MyClass alloc] init]; note that in this case we could better do by MyClass myObj = [[MyClass alloc] init]; because we know exactly what type of object is returned, but in case we wouldn't know, we could use id and call it a day.

Conclusion: Objective-C is actually type agnostic, which I was happy to find out. Provide types when possible and reasonable, but feel free to use id type instead.

Initialization

Say we have class Calculator and we want to instantiate it. We'd write this crypt: [[Calculator alloc] init] Why alloc and why init? That's how it is, alloc is the first thing to do, which allocates memory, and init is the second thing to do, which sets up Class Instance I guess. What am I doing here, I miss sane programming languages so much.

Primitive structures as arrays/strings/etc

Literal String - @"foo bar";

Literal Array - @[@"foo", @"bar"];

Literal mutable Array - [@[@"foo", @"bar"] mutableCopy]; basically we first create immutable array and then call mutableCopy method on that to create and return an instance of a sub class which provides a mutable Array. I guess this snippet was the first time when I liked the Objective-C syntax for calling methods lol.

Empty immutable Array - [NSArray array];

Empty mutable Array - [NSMutableArray array];