Great post explaining the short circuit procedure that takes place with the double ampersand operator:
http://stackoverflow.com/questions/9299350/what-does-double-ampersand-do-in-this-program
Great post explaining the short circuit procedure that takes place with the double ampersand operator:
http://stackoverflow.com/questions/9299350/what-does-double-ampersand-do-in-this-program
After the joys of Ruby’s REPL environment, it feels a significant step backwards to go back to the Compile/Test cycle in Objective-C.
Bring on a REPL, Apple!
Until then, this is possibly the best option:
http://merbist.com/2012/05/04/macruby-on-ios-rubymotion-review/
Modules
@import optionally replaces the long, cumbersome process of:
#include
Link binary with libraries
and makes it a whole lot more scalable to build large applications. What it does is a semantic import rather than a textual inclusion.
To use go into Settings and enable BUT it requires iOS7 and the OS X 10.9 SDK.
Tagged Pointers
For small value objects, the object is stored in the pointer itself which makes it over 100x faster and 3x more space efficient.
BUT, public data structures are increasingly becoming private. i.e. use the APIs to introspect.
i.e. use object_getClass and isKindOfClass
Garbage Collection
ARC is increasingly being used in new frameworks. So, use it!
e.g. Xcode 5.0 now uses ARC.
To convert code use the ARC Migrator which does the heavy lifting.
See WWDC 2012 for more details on ARC.
New Memory Management Warnings
Improving CoreFoundation and ARC
NSString *string = (__bridge NSString *)CFDictionaryGetValue(_dict, @”key”);
The ARC compiler must reason about object lifetime.
This requires retain count “bridging” in and out of ARC.
+1 via CFBridgingRetain()
-1 via CFBridgingRelease()
0 via “__bridge” casts to avoid mistakes
Common CF APIs now allow implicit bridging.
Related sessions:
WWDC 2013: What’s New in the LLVM Compiler
WWDC 2013: Optimize Your Code Using LLVM
In Ruby (unlike Objective-C) nil and zero are different.
Here’s how to tell them apart:
unless var.nil? || var == 0
# …
end
http://stackoverflow.com/questions/252203/checking-if-a-variable-is-not-nil-and-not-zero-in-ruby
The version of compiler you’re running will affect all the software you compile.
Unfortunately, the default changes on the Mac. It dependsĀ on:
So, how does it affect the software?
For starters, you’ll find some just doesn’t compile. E.g. take a look at some of the problems installing Ruby under Lion:
So, here’s a small guide.
Compilers have a front-end, that contains the parser and semantic analysis for the programming language and generates an intermediate representation of your code, and a backend which takes the stuff the front-end produced, optimizes it, and generates assembly code.
clang’s (Objective-)C++ support is far from being complete so it calls llvm-gcc when it encounters a C++ source file. It also contains the static analyzer that is now integrated into Xcode. Some people say LLVM’s back-end generates better code than GCC’s but your mileage may vary. LLVM also supports link-time optimizations (which you can enable in Xcode’s project settings). They may produce faster code.
Apple wants to replace GCC with clang in the future because they have a policy against GPLv3 licensed code (GCC 4.2 is the last version that’s licensed under GPLv2).
Try it out:
gcc –version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
means you’re running gcc.
http://stackoverflow.com/questions/1551099/xcode-3-2-1-gcc-clang-and-llvm-demystification
Brief intro:
An enum (or enumerated data type) is the C way to define constants for fixed values (like the styles for a table cell).
This declares integer values (Objective-C will assign values by default starting with 0):
1 2 3 4 5 6 |
enum { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle }; |
This is an anonymous enum. We could give it a name or tag, e.g.
1 2 3 4 5 6 |
enum UITableViewCellStyle { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle }; |
but this isn’t required.
A typedef creates a new type definition with a name and type. The type goes first and the name last:
1 2 3 4 5 6 |
typedef enum { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle } UITableViewCellStyle; |
Note that this is the anonymous version of the two previous enums although the typedef does have a name – UITableViewCellStyle.
Way more here on typedef / enum:
http://stackoverflow.com/questions/707512/what-is-a-typedef-enum-in-objective-c
but a couple of short video explanations here:
enum
typedef
Note that with iOS6 we have the new NS_ENUM and NS_OPTIONS. E.g.
1 2 3 4 |
typedef NS_ENUM( NSUInteger, CarType ) { FourDoorCarType, TwoDoorCarType }; |
More on them here:
http://nshipster.com/ns_enum-ns_options/
Tips
Naming conventions:
1 2 |
[NSArray array] [NSArray arrayWithObjects:count:] |
See
http://stackoverflow.com/questions/1681684/object-allocation-from-class-method
however, note that the Autorelease stuff is no longer relevant due to ARC. E.g.
http://stackoverflow.com/questions/9752790/is-autorelease-redundant-when-using-arc-in-objective-c
Use NSSelectorFromString
See the Runtime Reference:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_9_section_3.html
and
http://stackoverflow.com/questions/112643/how-can-i-dynamically-create-a-selector-at-runtime-with-objective-c
E.g.
[NSException raise:@”Invalid key value” format:@”Key value: ‘%@’ is invalid”, key];
A note of caution:
In Objective-C, unlike many similar languages, you generally should try to avoid using exceptions for common error situations that may occur in normal operation.
http://stackoverflow.com/questions/324284/how-to-throw-an-exception-in-objective-c-cocoa
1. Put in a breakpoint and look in the Stack trace
2. Use this:
1 2 3 4 5 6 7 8 9 10 11 12 |
NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1]; // Example: 1 UIKit 0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163 NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"]; NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString componentsSeparatedByCharactersInSet:separatorSet]]; [array removeObject:@""]; NSLog(@"Stack = %@", [array objectAtIndex:0]); NSLog(@"Framework = %@", [array objectAtIndex:1]); NSLog(@"Memory address = %@", [array objectAtIndex:2]); NSLog(@"Class caller = %@", [array objectAtIndex:3]); NSLog(@"Function caller = %@", [array objectAtIndex:4]); NSLog(@"Line caller = %@", [array objectAtIndex:5]); |
http://stackoverflow.com/questions/1451342/objective-c-find-caller-of-method