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
39,626
questions
1408
votes
39
answers
514k
views
How can I check for an active Internet connection on iOS or macOS?
I would like to check to see if I have an Internet connection on iOS using the Cocoa Touch libraries or on macOS using the Cocoa libraries.
I came up with a way to do this using an NSURL. The way I ...
1323
votes
82
answers
1.0m
views
Xcode - How to fix 'NSUnknownKeyException', Reason: "… this class is not key value coding-compliant for the key X" error?
I'm trying to link a UILabel with an IBOutlet created in my class.
My application is crashing with the following error"
***
Terminating app due to uncaught exception
'NSUnknownKeyException', ...
1047
votes
14
answers
448k
views
Constants in Objective-C
I'm developing a Cocoa application, and I'm using constant NSStrings as ways to store key names for my preferences.
I understand this is a good idea because it allows easy changing of keys if ...
774
votes
20
answers
386k
views
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?
723
votes
10
answers
265k
views
Cocoa Autolayout: content hugging vs content compression resistance priority
I can't find a clear answer on Apple documentation regarding Cocoa Autolayout about the difference between content hugging and compression resistance.
Can somebody explain their usages and ...
718
votes
16
answers
172k
views
@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 ...
659
votes
9
answers
179k
views
What does the NS prefix mean?
Many classes in Cocoa/Cocoa Touch have the NS prefix. What does it mean?
631
votes
14
answers
230k
views
Cocoa: What's the difference between the frame and the bounds?
UIView and its subclasses all have the properties frame and bounds. What's the difference?
572
votes
8
answers
165k
views
@synthesize vs @dynamic, what are the differences?
What are the differences between implementing a @property with @dynamic or @synthesize?
452
votes
13
answers
147k
views
How to print out the method name and line number and conditionally disable NSLog?
I'm doing a presentation on debugging in Xcode and would like to get more information on using NSLog efficiently.
In particular, I have two questions:
is there a way to easily NSLog the current ...
427
votes
13
answers
147k
views
throwing an exception in objective-c/cocoa
What's the best way to throw an exception in objective-c/cocoa?
418
votes
9
answers
109k
views
Is It Possible to NSLog C Structs (Like CGRect or CGPoint)?
I want to be able to debug C structures without having to explicitly type every property that they consist of.
i.e. I want to be able to do something like this:
CGPoint cgPoint = CGPointMake(0,0);
...
394
votes
9
answers
209k
views
How do I convert NSMutableArray to NSArray?
How do I convert NSMutableArray to NSArray in objective-c?
363
votes
25
answers
51k
views
Exception thrown in NSOrderedSet generated accessors
On my Lion app, I have this data model:
The relationship subitems inside Item is ordered.
Xcode 4.1 (build 4B110) has created for me the file Item.h, Item.m, SubItem.h and SubItem.h.
Here is the ...
363
votes
12
answers
189k
views
Best way to define private methods for a class in Objective-C
I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.
I understand there may be several conventions and ...
354
votes
6
answers
116k
views
Difference between objectForKey and valueForKey?
What is the difference between objectForKey and valueForKey?
I looked both up in the documentation and they seemed the same to me.
346
votes
33
answers
124k
views
What are best practices that you use when writing Objective-C and Cocoa? [closed]
I know about the HIG (which is quite handy!), but what programming practices do you use when writing Objective-C, and more specifically when using Cocoa (or CocoaTouch).
341
votes
6
answers
537k
views
How do I use NSTimer?
How do I use an NSTimer? Can anyone give me step by step instructions?
338
votes
89
answers
241k
views
Hidden Features of Xcode
With a huge influx of newbies to Xcode, I'm sure there are lots of Xcode tips and tricks to be shared.
What are yours?
336
votes
10
answers
138k
views
NSString property: copy or retain?
Let's say I have a class called SomeClass with a string property name:
@interface SomeClass : NSObject
{
NSString* name;
}
@property (nonatomic, retain) NSString* name;
@end
I understand that ...
334
votes
17
answers
77k
views
What is the best way to unit test Objective-C code?
What frameworks exist to unit test Objective-C code? I would like a framework that integrates nicely with Apple Xcode.
317
votes
4
answers
95k
views
Core Data vs SQLite 3 [closed]
I am already quite familiar with relational databases and have used SQLite (and other databases) in the past. However, Core Data has a certain allure, so I am considering spending some time to learn ...
306
votes
7
answers
97k
views
How do you print out a stack trace to the console/log in Cocoa?
I'd like to log the call trace during certain points, like failed assertions, or uncaught exceptions.
290
votes
12
answers
219k
views
CSS Printing: Avoiding cut-in-half DIVs between pages?
I'm writing a plug-in for a piece of software that takes a big collection of items and pops them into HTML in a WebView in Cocoa (which uses WebKit as its renderer, so basically you can assume this ...
282
votes
3
answers
56k
views
What does "@private" mean in Objective-C?
What does @private mean in Objective-C?
278
votes
9
answers
216k
views
How to update a single pod without touching other dependencies
I understand that the following command will update a single pod: pod update <podname>. However this also updates the dependencies of other pods (pods that were not included in the update ...
261
votes
46
answers
175k
views
"Unknown class <MyClass> in Interface Builder file" error at runtime
Even though Interface Builder is aware of a MyClass, I get an error when starting the application.
This happens when MyClass is part of a library, and does not happen if I compile the class directly ...
260
votes
3
answers
119k
views
Objective-C: Extract filename from path string
When I have NSString with /Users/user/Projects/thefile.ext I want to extract thefile with Objective-C methods.
What is the easiest way to do that?
247
votes
33
answers
169k
views
Delete/Reset all entries in Core Data?
Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.
Edit
I'm looking to do this programmatically so that a user ...
231
votes
10
answers
161k
views
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.
212
votes
29
answers
536k
views
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 ...
211
votes
12
answers
136k
views
Execute a terminal command from a Cocoa app
How can I execute a terminal command (like grep) from my Objective-C Cocoa application?
209
votes
8
answers
64k
views
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,...
207
votes
30
answers
180k
views
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 ...
205
votes
11
answers
152k
views
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)...
201
votes
4
answers
117k
views
Check that an email address is valid on iOS [duplicate]
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 ...
200
votes
20
answers
154k
views
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?
...
195
votes
5
answers
98k
views
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?
193
votes
12
answers
79k
views
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 ...
180
votes
8
answers
102k
views
How to convert CFStringRef to NSString?
NSString *aNSString;
CFStringRef aCFString;
aCFString = CFStringCreateWithCString(NULL, [aNSString UTF8String], NSUTF8StringEncoding);
aCFString = CFXMLCreateStringByUnescapingEntities(NULL, aCFString,...
178
votes
4
answers
50k
views
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:...
178
votes
13
answers
43k
views
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 ...
177
votes
7
answers
147k
views
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 ...
176
votes
4
answers
47k
views
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 ...
176
votes
6
answers
90k
views
How to create a Menubar application for Mac
EDIT: This is a nice ready-made menubar application here (github source) by this answer.
I was wondering how to make a menubar application, what are the requirements for that to do so?
I saw a ...
173
votes
10
answers
78k
views
Regular expressions in an Objective-C Cocoa application
Initial Googling indicates that there's no built-in way to do regular expressions in an Objective-C Cocoa application.
So four questions:
Is that really true?
Are you kidding me?
Ok, then is there a ...
170
votes
7
answers
107k
views
What's the difference between "bundle display name" and "bundle name" in cocoa application's info plist
Here is a good question:
Bundle Name, Executable Name, Product Name...anything else?
but I just wonder about the difference between "bundle display name" and "bundle name" in cocoa application's ...
159
votes
9
answers
53k
views
How does an underscore in front of a variable in a cocoa objective-c class work?
I've seen in a few iPhone examples that attributes have used an underscore _ in front of the variable. Does anyone know what this means? Or how it works?
An interface file I'm using looks like:
@...
154
votes
20
answers
97k
views
Generate a random alphanumeric string in Cocoa
I want to call a method, pass it the length and have it generate a random alphanumeric string.
Are there any utility libraries out there that may have a bunch of these types of functions?
154
votes
4
answers
113k
views
Objective-C - Remove last character from string
In Objective-C for iOS, how would I remove the last character of a string using a button action?