smallseo.info

Objective-C

A few code snippets from my Xcode arsenal

Creating an abstract class in Objective C

I'm originally a Java programmer who now works with Objective-C. I'd like to create an abstract class but that doesn't appear to be possible in Objective-C. Is this possible?

If not, how close to an abstract class can I get in Objective-C?


Source: (StackOverflow)

How to convert an NSString into an NSNumber

How can I convert a NSString containing a number of any primitive data type (e.g. int, float, char, unsigned int, etc.)? The problem is, I don't know which number type the string will contain at runtime.

I have an idea how to do it, but I'm not sure if this works with any type, also unsigned and floating point values:

long long scannedNumber;
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanLongLong:&scannedNumber]; 
NSNumber *number = [NSNumber numberWithLongLong: scannedNumber];

Thanks for the help.


Source: (StackOverflow)

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:

Random.nextInt(74)

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:@"http://www.google.com"]];
    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 {
    kCircle,
    kRectangle,
    kOblateSpheroid
} ShapeType;

What is really being declared here?


Source: (StackOverflow)