objective-c

super viewDidUnload in Cocoa

In the function viewDidUnload it is initially empty. However, I'm following a tutorial where at the end of the function they write [super viewDidUnload]. I noticed that in the dealloc function, [super dealloc] is automatically written at the end. Why isn't it automatically written at the end of viewDidUnload? Does it make a difference? W...

accessing Objective-C nested array element

I have an array initialized - (void) viewDidLoad { NSArray *myArray = [NSArray arrayWithObjects: [NSArray arrayWithObjects:@"item 1-1", @"item 1-2", nil], [NSArray arrayWithObjects:@"item 2-1", @"item 2-2", nil], [NSArray arrayWithObjects:@"item 3-1", @"item 3-2", nil], ...

obj-c NSString and alloc / retain / release

This is probably a question that is more about object alloc/retain/release, but I'll use NSString as an example. I'm aware that I can do: NSString* myString = [[NSString alloc] initWithString:@"Test"]; to essentially allocate and initialize a string referenced by my variable myString which I should later call [myString release] upon....

How to write to the System.keychain?

I'm writing a preferences panel and I need to either update or store a password in the System.keychain for others to find. Here's the code I have so far: Boolean addOrUpdateKey(NSString *service, NSString *key) { OSStatus retVal; SecKeychainRef systemKeychainRef; SecKeychainItemRef kcItem; UInt32 pwSize = 0; char *password = N...

How can I use an NSArray as a global constant?

I'm using a set of Constant.m files, one per target, to define specific things for each target. For example: // Constants.h extern NSString * const kDatabaseFileName; //Constants.m NSString * const kDatabaseFileName = @"target_one.sqlite"; I'd also like to define an NSArray for each of my targets: NSArray * const kLabelNames = [[NSAr...

EXC_BAD_ACCESS when replaceObject called on an NSMutableArray

Hi All I am attempting to replace an object in an NSMutableArray. Elsewhere in my class I am doing it successfully. Below is an example of the code failure. NSNumber* newObject = [NSNumber numberWithDoulble:myCalculation]; NSLog(@"Old object at 12:%@",[myMisbehavingArray objectAtIndex:12]; [myMisbehavingArray replaceObjectAtIndex:12 wi...

How to release objects stored in an array?

Please look at the code below and suggest the best approach. I can't quite tell whether the code is correct. When adding objects to arrays, do they get a retain count? In the second function, am I releasing the local variable "mySubview" or the original object? // this is a class property myArray = [[NSMutableArray alloc] init]...

NSNumberFormatter and 'th' 'st' 'nd' 'rd' (ordinal) number endings

Is there a way to use NSNumberFormatter to get the 'th' 'st' 'nd' 'rd' number endings? EDIT: Looks like it does not exist. Here's what I'm using. +(NSString*)ordinalNumberFormat:(NSInteger)num{ NSString *ending; int ones = num % 10; int tens = floor(num / 10); tens = tens % 10; if(tens == 1){ ending = @"th...

Interface Builder and the preprocessor

Is there any way to force/cajole/encourage Interface Builder into running the C preprocessor when scanning source files for IBOutlet references? It's a tricky problem, I can see, since in an ideal world it would need to preprocess the file with the same context that the project would, ie with the precompiled headers etc (this is much mo...

How to put a UILabel ontop of a UIToolbar?

I have a UILabel that shows a character count as the user types into a textfield. Currently it is sitting behind a translucent UIToolbar. I would like the UILabel to be ontop of the UIToolbar. How can I accomplish this? ...

Cocoa -- toggling a BOOL without repeating its name.

If a BOOL has a nice short name, it's easy enough to write: myBOOL = !myBOOL; But what if the BOOL has a long name? objectWithLongishName.memberWithLongishName.submember.myBOOL = !(objectWithLongishName.memberWithLongishName.submember.myBOOL); . . . does not look so pretty. I'm wondering if there is an easy way to toggle the BOO...

XCode warning: argument 3 discards qualifiers from pointer target type

Hello, XCode is giving me a warning that I can't figure out why. @interface SFHFKeychainUtils : NSObject { } + (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error; @end In another class I call storeUse...

ModalViewController within a UINavigationController not animating on dismissal

Hi - I've got an application with several modal view controllers. Most of them work fine except for one that is within a UINavigationController (so that I can show a navbar). When this modal view controller is displayed it animates fine but when dismissed it does not animate. I'd really like it to animate. Any thoughts? Thanks much...

How to read cell data from an Excel document with objective-c

I'm building an iPhone app that has to download an Excel (.xls) file from a website and perform a string search of the spreadsheet data. I've done this in C#, but I have no idea how to do this in objective-c. How do I access the individual cells using objective-c? ...

How can I change the font color of a UIBarButton item?

I want to change the color to red. ...

How do i terminate an iPhone application gracefully in code

What is the proper way of ending an application on the iPhone when you are finished with it? thanks, anton ...

How can I determine if a user has pressed on a UITableViewCell for 2 seconds?

I am using gesture recognizers: Initialize in viewDidLoad: UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.view addGestureRecognizer:longPressRecognizer]; This is what longPress looks like: - (void)longPress:(UILongPressGestureRecogni...

How do include and call sample code methods in Objective-C?

iPhone/Mac "play a sound class": link text I find an awful lot of great objective-c classes and code samples here... and elsewhere. I successfully create the .h and .m files, but how do I call them from my existing code? Where do I put the @class or #import statements? How do I call the methods? What if I need to play 2-3 different...

Objective-C Moving Items Between Arrays (Memory Management)

Is there any practical difference between the following two code snippets: NSObject * obj = [[_mutableArrayOne objectAtIndex:i] retain]; [_mutableArrayOne removeObject:obj]; [_mutableArrayTwo addObject:obj]; [obj release]; and NSObject * obj = [_mutableArrayOne objectAtIndex:i]; [_mutableArrayTwo addObject:obj]; [_mutableArrayOne remo...

No declaration of property error in Objective-C

I wonder what is wrong with this? .h file: typedef enum { N4LoupeTypeRound, N4LoupeTypeRectangle, } N4LoupeType; @interface N4LoupeLayer : CALayer { N4LoupeType _type; UIView *_originalView; CALayer *_mask; CALayer *_overlay; } @property (nonatomic) N4LoupeType type; @property (nonatomic, assign) UIView *origi...