Objective-C
A few code snippets from my Xcode arsenal
I'm developing a Cocoa application, and I'm using constant NSString
s as ways to store key names for my preferences.
I understand this is a good idea, because it allows easy changing of keys if necessary. Plus, it's the whole 'separate your data from your logic' notion.
Anyway, is there a good way to make these constants defined once for the whole application? I'm sure that there's an easy and intelligent way, but right now my classes just redefine the ones they use.
Source: (StackOverflow)
How can I create a basic UIButton
programmatically? For example in my view controller, when executing the viewDidLoad
method, three UIButton
s will be created dynamically and its layout or properties are set.
Source: (StackOverflow)
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)
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)
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)
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)
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)
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)
This question already has an answer here:
There are a lot of questions about this problem on SO:
But really no one answer works for me.
How do I capture an image programmatically without the camera shutter sound activating?
Source: (StackOverflow)
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)
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)