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

UITextField text change event

From proper way to do uitextfield text change call back: I catch the characters sent to a UITextField control something like this: // Add a “textFieldDidChange” notification method to the text field control. In Objective-C: [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; In Swift: textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged) Then in the textFieldDidChange method you can examine the … Read more

Send and receive messages through NSNotificationCenter in Objective-C?

@implementation TestClass – (void) dealloc { // If you don’t remove yourself as an observer, the Notification Center // will continue to try and send notification objects to the deallocated // object. [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } – (id) init { self = [super init]; if (!self) return nil; // Add this instance of … Read more

What does the NS prefix mean?

The original code for the Cocoa frameworks came from the NeXTSTEP libraries Foundation and AppKit (those names are still used by Apple’s Cocoa frameworks), and the NextStep engineers chose to prefix their symbols with NS. Because Objective-C is an extension of C and thus doesn’t have namespaces like in C++, symbols must be prefixed with … 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

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