Creating an abstract class in Objective-C

Typically, Objective-C class are abstract by convention only—if the author documents a class as abstract, just don’t use it without subclassing it. There is no compile-time enforcement that prevents instantiation of an abstract class, however. In fact, there is nothing to stop a user from providing implementations of abstract methods via a category (i.e. at … Read more

@synthesize vs @dynamic, what are the differences?

@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime). Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to … Read more

How to convert an NSString into an NSNumber

Use an NSNumberFormatter: NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; f.numberStyle = NSNumberFormatterDecimalStyle; NSNumber *myNumber = [f numberFromString:@”42″]; If the string is not a valid number, then myNumber will be nil. If it is a valid number, then you now have all of the NSNumber goodness to figure out what kind of number it actually is.

dispatch_after – GCD in Swift?

I use dispatch_after so often that I wrote a top-level utility function to make the syntax simpler: func delay(delay:Double, closure:()->()) { dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)) ), dispatch_get_main_queue(), closure) } And now you can talk like this: delay(0.4) { // do stuff } Wow, a language where you can improve the language. What could … Read more

Location Services not working in iOS 8

I ended up solving my own problem. Apparently in iOS 8 SDK, requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates. There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my … Read more

@class vs. #import

If you see this warning: warning: receiver ‘MyCoolClass’ is a forward class and corresponding @interface may not exist you need to #import the file, but you can do that in your implementation file (.m), and use the @class declaration in your header file. @class does not (usually) remove the need to #import files, it just … Read more

Generating random numbers in Objective-C

You should use the arc4random_uniform() function. It uses a superior algorithm to rand. You don’t even need to set a seed. #include <stdlib.h> // … // … int r = arc4random_uniform(74); The arc4random man page: NAME arc4random, arc4random_stir, arc4random_addrandom — arc4 random number generator LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <stdlib.h> u_int32_t arc4random(void); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)