A few code snippets from my Xcode arsenal

How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int/double/float?

Source: (StackOverflow)

@synthesize vs @dynamic, what are the differences?

What are the differences between implementing a @property with @dynamic or @synthesize?

Source: (StackOverflow)

How do I test if a string is empty in Objective C?

How do I test if an NSString is empty in Objective C?

Source: (StackOverflow)

Location Services not working in iOS 8

My app that worked fine on iOS 7 doesn't work with the iOS 8 SDK.

CLLocationManager doesn't return a location, and I don't see my app under Settings -> Location Services either. I did a Google search on the issue, but nothing came up. What could be wrong?

Source: (StackOverflow)

How do I create delegates in Objective-C?

I know how delegates work, and I know how I can use them.

But how do I create them?

Source: (StackOverflow)

How to check iOS version?

I want to check if the iOS version of the device is greater than 3.1.3 I tried things like:

[[UIDevice currentDevice].systemVersion floatValue]

but it does not work, I just want a:

if (version > 3.1.3) { }

How can I achieve this?

Source: (StackOverflow)

Generating random numbers in Objective-C

I'm a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:


I'm not interested in a discussion about seeds or true randomness, just how you accomplish the same task in Objective-C. I've scoured Google, and there just seems to be lots of different and conflicting bits of information.

Source: (StackOverflow)

@class vs. #import

It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header to avoid any circular inclusions. I also understand that an #import is a simple ifndef so that an include only happens once.

My inquiry is this: When does one use #import and when does one use @class? Sometimes if I use a @class declaration, I see a common compiler warning such as the following:

warning: receiver 'FooController' is a forward class and corresponding @interface may not exist.

Would really love to understand this, versus just removing the @class forward-declaration and throwing an #import in to silence the warnings the compiler is giving me.

Source: (StackOverflow)

Passing Data between View Controllers

I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following.

I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with a tableview controller and I have enabled multiple selections.

My question is, how do I transfer the data from one view to another? I will be holding the selections on the tableview in an array, but how do I then pass that back to the previous data entry form view so it can be saved along with the other data to core data on submission of the form?

I have surfed around and seen some people declare an array in the app delegate. I read something about Singletons but don't understand what these are and I read something about creating a data model.

What would be the correct way of performing this and how would I go about it?

Source: (StackOverflow)

How to check for an active Internet Connection on iPhone SDK?

I would like to check to see if I have an Internet connection on the iPhone using the Cocoa Touch libraries.

I came up with a way to do this using an NSURL. The way I did it seems a bit unreliable (because even Google could one day be down and relying on a 3rd party seems bad) and while I could check to see for a response from some other websites if Google didn't respond, it does seem wasteful and an unnecessary overhead on my application.

- (BOOL) connectedToInternet
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@""]];
    return ( URLString != NULL ) ? YES : NO;

Is what I have done bad? (Not to mention stringWithContentsOfURL is deprecated in 3.0) And if so what is a better way to accomplish this?

Source: (StackOverflow)

Shortcuts in Objective-C to concatenate NSStrings

Are there any shortcuts to (stringByAppendingString:) string concatenation in Objective-C, or shortcuts for working with NSString in general?

For example, I'd like to make

NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];

something more like

string myString = "This";
string test = myString + " is just a test";

Source: (StackOverflow)

What is a typedef enum in Objective-C?

I don't think I fundamentally understand what an enum is, and when to use it.

For example:

typedef enum {
} ShapeType;

What is really being declared here?

Source: (StackOverflow)

performSelector may cause a leak because its selector is unknown

I'm getting the following warning by the ARC compiler:

"performSelector may cause a leak because its selector is unknown".

Here's what I'm doing:

[_controller performSelector:NSSelectorFromString(@"someMethod")];

Why do I get this warning? I understand the compiler can't check if the selector exists or not, but why would that cause a leak? And how can I change my code so that I don't get this warning anymore?

Source: (StackOverflow)

How can I disable ARC for a single file in a project?

I am using ARC successfully in my project. However, I have encountered a few files (e.g., in unit tests and mock objects) where the rules of ARC are a little more fragile right now. I recall hearing that there was a way to disable ARC on a per-file basis, though I have been unable to find this option.

Is this possible? How do I disable ARC on a per-file basis?

Source: (StackOverflow)

What's the difference between the atomic and nonatomic attributes?

What do atomic and nonatomic mean in property declarations?

@property(nonatomic, retain) UITextField *userName;
@property(atomic, retain) UITextField *userName;
@property(retain) UITextField *userName;

What is the operational difference between these three?

Source: (StackOverflow)