Questions tagged [nsobject]

NSObject is the root class of most Objective-C class hierarchies; it has no superclass. From NSObject, other classes inherit a basic interface to the runtime system for the Objective-C language, and its instances obtain their ability to behave as objects.

Although it is not strictly an abstract class, NSObject is virtually one. By itself, an NSObject instance cannot do anything useful beyond being a simple object. To add any attributes and logic specific to the program, you must create one or more classes inheriting from NSObject or from any other class derived from NSObject. NSObject adopts the NSObject protocol. The NSObject protocol allows for multiple root objects. For example, NSProxy, the other root class, does not inherit from NSObject but adopts the NSObject protocol so that it shares a common interface with other Objective-C () objects.

NSObject is the name not only of a class but of a protocol. Both are essential to the definition of an object in Cocoa (). The NSObject protocol specifies the basic programmatic interface required of all root classes in Cocoa. Thus not only the NSObject class adopts the identically named protocol, but the other Cocoa root class, NSProxy, adopts it as well. The NSObject class further specifies the basic programmatic interface for any Cocoa object that is not a proxy object.

The design of Objective-C uses a protocol such as NSObject in the overall definition of Cocoa objects (rather than making the methods of the protocol part of the class interface) to make multiple root classes possible. Each root class shares a common interface, as defined by the protocols they adopt.

More Information : NSObject Class reference

899 questions
124
votes
2 answers

NSObject +load and +initialize - What do they do?

I'm interested in understanding the circumstances leading a developer to override +initialize or +load. Documentation makes it clear these methods are called for you by the Objective-C runtime, but that's really all that is clear from the…
edelaney05
  • 6,822
  • 6
  • 41
  • 65
59
votes
4 answers

How do I store an NSRange in a NSMutableArray or other container?

Here's what I want to do: NSRange r = NSMakeRange(0,5); id a = [NSMutableArray a]; [a addObject: r]; // but NSRange is not a NSObject * With a boolean, I'd use code like this: [a addObject: [NSNumber numberWithBool: YES]]; or with an integer: [a…
Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
59
votes
4 answers

NSObject subclass in Swift: hash vs hashValue, isEqual vs ==

When subclassing NSObject in Swift, should you override hash or implement Hashable? Also, should you override isEqual: or implement the == operator?
user4691305
41
votes
3 answers

Subclassing NSObject in Swift - Best Practice with Initializers

Here is the layout of an example Class, can someone guide me on what's best practice when creating a subclass of NSObject? class MyClass: NSObject { var someProperty: NSString! = nil override init() { self.someProperty = "John" …
Woodstock
  • 22,184
  • 15
  • 80
  • 118
35
votes
3 answers

What is __NSArrayI and __NSArrayM? How to convert to NSArray?

What is __NSArrayI and __NSArrayM? __NSArrayI(or M) cause "unrecognized selector" error. How to convert to NSArray? I did test to parse json, twitter api. http://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=twitterapi ==> works fine.…
ChangUZ
  • 5,380
  • 10
  • 46
  • 64
33
votes
2 answers

NSObject description and debugDescription

I have seen mentioning of description and debugDescription for debugging purposes, but have yet seen precise explanations of when to use which and under what conditions they may produce different results. NSObject's documentation also doesn't have…
Boon
  • 40,656
  • 60
  • 209
  • 315
32
votes
3 answers

What is the NSObject isEqual: and hash default function?

I have a database model class that is a NSObject. I have a set of these objects in a NSMutableArray. I use indexOfObject: to find a match. Problem is the model object's memory address changes. So I am overriding the hash method to return the model's…
Brenden
  • 7,708
  • 11
  • 61
  • 75
30
votes
3 answers

Objective-C: Why check nil before respondsToSelector:?

I've seen code like: if (delegate != nil && [delegate respondsToSelector:@selector(doSomething)]) ... But, sending a message to nil just returns nil (which evaluates to NO), so why not just do: if ([delegate…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
30
votes
2 answers

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject

The FooBar class below has to override the == function of the Equatable type. However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is…
Crashalot
  • 33,605
  • 61
  • 269
  • 439
25
votes
4 answers

Removing Duplicates From Array of Custom Objects Swift

I have a custom class defined as follows : class DisplayMessage : NSObject { var id : String? var partner_image : UIImage? var partner_name : String? var last_message : String? var date : NSDate? } Now I have an array myChats = [DisplayMessage]?.…
Alk
  • 5,215
  • 8
  • 47
  • 116
23
votes
2 answers

'Set' does not have a member named 'anyObject." - Xcode 6.3

I'm checking to see if an element has been selected. func touchesBegan(touches: Set, withEvent event: UIEvent) { // First, see if the game is in a paused state if !gamePaused { // Declare the touched symbol and its…
Willie
  • 231
  • 1
  • 2
  • 3
23
votes
5 answers

Why subclass NSObject?

What is the purpose/use of NSObject in Objective-C? I see classes that extend NSObject like this: @interface Fraction : NSObject In C++ or Java, we don't use any variables like NSObject even though we have preprocessor directives and import…
user185590
  • 451
  • 2
  • 7
  • 11
20
votes
3 answers

Swift 3: subclassing NSObject or not?

I have read some posts like this one about the difference between subclassing NSObject in Swift or just having its native base class with no subclassing. But they all are a bit old posts, and I am not clear about this topic. When should you subclass…
AppsDev
  • 12,319
  • 23
  • 93
  • 186
20
votes
5 answers

Class hierarchy of NSObject

I trying to understand class hierarchy. What are the sub classes of NSObject. Is there any graphical representation of the hierarchy?
mind
  • 209
  • 1
  • 2
  • 9
20
votes
4 answers

Must Protocols Conform To The NSObject Protocol?

The NSObject protocol comes with the stock protocol templates, but it doesn't appear to be all that necessary for actual implementations of the protocol. Leaving it out seems to change absolutely nothing. So, is it really necessary for a protocol…
CodaFi
  • 43,043
  • 8
  • 107
  • 153
1
2 3
59 60