Questions tagged [cocoa]

Cocoa is Apple's application-development framework for macOS, consisting of Foundation, Application Kit, and Core Data. Use the "cocoa-touch" tag for iOS questions.

Cocoa is Apple's application-development framework for macOS, consisting of Foundation, Application Kit (AppKit) and Core Data.

The Cocoa frameworks consist of libraries, APIs and runtimes that form the development layer for all of macOS.

Related tags

Debugging Techniques

Cocoa programs can be debugged with many techniques and tools, both general and specific to Objective-C, Xcode and macOS.

External Resources

General Resources on SO

Memory Management

More information

39610 questions
229
votes
10 answers

How do I declare class-level properties in Objective-C?

Maybe this is obvious, but I don't know how to declare class properties in Objective-C. I need to cache per-class a dictionary and wonder how put it in the class.
mamcx
  • 15,916
  • 26
  • 101
  • 189
211
votes
12 answers

Execute a terminal command from a Cocoa app

How can I execute a terminal command (like grep) from my Objective-C Cocoa application?
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
210
votes
29 answers

Xcode build failure "Undefined symbols for architecture x86_64"

An Xcode beginner's question: It is my first experience with Xcode 4.6.3. I am trying to write a very simple console program, that searches for paired BT devices and prints them to an NSLog. It builds with the following error: Undefined symbols for…
RisingSun
  • 2,407
  • 2
  • 16
  • 11
208
votes
8 answers

Why can't code inside unit tests find bundle resources?

Some code I am unit testing needs to load a resource file. It contains the following line: NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"txt"]; In the app it runs just fine, but when run by the unit testing framework…
benzado
  • 82,288
  • 22
  • 110
  • 138
207
votes
30 answers

iOS - Build fails with CocoaPods cannot find header files

I have an iOS project using CocoaPods. Everything was working smoothly until another developer started to work on the same project. He made some changes (only to code as far as I know) and made a new branch in the repo. I have checked out his branch…
Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
205
votes
11 answers

How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

I have a UIImage (Cocoa Touch). From that, I'm happy to get a CGImage or anything else you'd like that's available. I'd like to write this function: - (int)getRGBAFromImage:(UIImage *)image atX:(int)xx andY:(int)yy { // [...] // What do I want…
Olie
  • 24,597
  • 18
  • 99
  • 131
201
votes
4 answers

Check that an email address is valid on iOS

Possible Duplicate: Best practices for validating email address in Objective-C on iOS 2.0? I am developing an iPhone application where I need the user to give his email address at login. What is the best way to check if an email address is a…
raaz
  • 12,410
  • 22
  • 64
  • 81
198
votes
20 answers

Best way to remove from NSMutableArray while iterating?

In Cocoa, if I want to loop through an NSMutableArray and remove multiple objects that fit a certain criteria, what's the best way to do this without restarting the loop each time I remove an object? Thanks, Edit: Just to clarify - I was looking for…
Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
193
votes
12 answers

What's the Best Way to Shuffle an NSMutableArray?

If you have an NSMutableArray, how do you shuffle the elements randomly? (I have my own answer for this, which is posted below, but I'm new to Cocoa and I'm interested to know if there is a better way.) Update: As noted by @Mukesh, as of iOS 10+…
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
191
votes
5 answers

NULL vs nil in Objective-C

In observeValueForKeyPath:ofObject:change:context: - why do the docs use NULL instead of nil when not specifying a context pointer?
erikprice
  • 6,240
  • 3
  • 30
  • 40
178
votes
8 answers

How to convert CFStringRef to NSString?

NSString *aNSString; CFStringRef aCFString; aCFString = CFStringCreateWithCString(NULL, [aNSString UTF8String], NSUTF8StringEncoding); aCFString = CFXMLCreateStringByUnescapingEntities(NULL, aCFString, NULL); How can I get a new NSString from…
papr
  • 4,717
  • 5
  • 30
  • 38
178
votes
4 answers

What's the optimum way of storing an NSDate in NSUserDefaults?

There's two ways of storing an NSDate in NSUserDefaults that I've come across. Option 1 - setObject:forKey: // Set NSDate *myDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"]; // Get NSDate *myDate =…
John Gallagher
  • 6,208
  • 6
  • 40
  • 75
178
votes
13 answers

What is the best way to solve an Objective-C namespace collision?

Objective-C has no namespaces; it's much like C, everything is within one global namespace. Common practice is to prefix classes with initials, e.g. if you are working at IBM, you could prefix them with "IBM"; if you work for Microsoft, you could…
Mecki
  • 125,244
  • 33
  • 244
  • 253
177
votes
7 answers

How to initialise a string from NSData in Swift

I have been trying to initialise a string from NSData in Swift. In the NSString Cocoa Documentation Apple is saying you have to use this: init(data data: NSData!, encoding encoding: UInt) However Apple did not include any example for usage or…
ahmed
  • 14,316
  • 30
  • 94
  • 127
176
votes
4 answers

Why are Objective-C delegates usually given the property assign instead of retain?

I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to understand a fundamental Objective-C concept of assigning delegates the 'assign' property vs 'retain'. Note, the both are the same in a garbage collected…
anon