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
12
votes
3 answers

NSObject custom init with object/parameters

What i'm trying to accomplish is something like Person *person1 = [[Person alloc]initWithDict:dict]; and then in the NSObject "Person", have something like: -(void)initWithDict:(NSDictionary*)dict{ self.name = [dict objectForKey:@"Name"]; …
Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92
12
votes
2 answers

What can the "Object" in Storyboard's Object Library do?

Short Question There is a thing called Object in XCode's interface builder's Object Library. I wonder what kind of task can be achieved by using this "Object". Be specific, it is an NSObject in Storyboard (or xib). It's described as follow:…
Greg Wang
  • 1,357
  • 1
  • 13
  • 17
11
votes
3 answers

Bug with equals operator and NSObjects in Swift 2.0?

Ok, something strange is happening when writing your own equals operator for NSObject subclasses in Swift 2.0 like this: func ==(lhs: MyObject, rhs: MyObject) -> Bool { return lhs.identifier == rhs.identifier } For a class that looks like…
Tom van Zummeren
  • 9,130
  • 12
  • 52
  • 63
11
votes
3 answers

Safe way to create singleton with init method in Objective-C

I would like to take the GCD approach of using shared instances to the next step so I created the following code: @implementation MyClass static id sharedInstance; #pragma mark Initialization + (instancetype)sharedInstance { static…
Ondrej Rafaj
  • 4,342
  • 8
  • 42
  • 65
11
votes
4 answers

Why use (id) in a method signature when (NSObject *) would be more precise?

Whenever I implement a method in my own code that can accept or return objects of more than one class, I always try to use the most specific superclass available. For example, if I were going to implement a method that might return an NSArray * or…
cduhn
  • 17,818
  • 4
  • 49
  • 65
11
votes
2 answers

Objective-C setValue:forKey on c primitive types

I'm trying to teach myself objective-c using the big nerd ranch book, it is a really great book but certain aspects confuse me. The current chapter is talking about using setValue:forKey function which I understand is a method defined in NSObject.…
koalamo
  • 199
  • 3
  • 11
11
votes
3 answers

What is the purpose of the -self method in NSObject-conformant classes?

That's it. Why would anyone want (at least as a public API) a method such as that? Is there any practical use for it?
Matoe
  • 2,742
  • 6
  • 33
  • 52
11
votes
2 answers

performSelector:withObject: and its retain behavior

This is an already answer question within SO but I cannot find it in the Apple documentation anywhere. Could you point me in the right direction? Within the following topics Do I have to retain an object before passing it to…
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
10
votes
3 answers

How to store custom class using CoreData

I have a class extending NSObject. It is consisting of a few float variables. I want to store this class in core data. In the data model, It seems that the most probable option is to turn this class to a binary data in order to store it using…
qutaibah
  • 313
  • 1
  • 4
  • 11
10
votes
2 answers

Categories on NSObject -- keeping it safe

Apple has this to say: Categories of the Root Class A category can add methods to any class, including the root class. Methods added to NSObject become available to all classes that are linked to your code. Adding methods to the root class with a…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
10
votes
4 answers

How Does AnyObject Conform to NSObjectProtocol?

This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error. Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will…
JAL
  • 41,701
  • 23
  • 172
  • 300
10
votes
2 answers

Class type Objective C

In the NSObject protocol, it defines a method that is similar to this: -(Class) class What type of object is the Class object? Or is it even an object? What can I do with the object? Can I get the base class or adopted protocols?
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
10
votes
4 answers

Cannot convert value of type '[String : String?]' to expected argument type '[NSObject : AnyObject]?'

When I use Parse 1.8.5 to upload data to Parse, this cloud code has compile error in "params" that I cannot debug it. let params = ["phoneNumber" : userPhoneNumber, "username": username, "password":…
ios killers
  • 109
  • 1
  • 1
  • 3
10
votes
4 answers

iOS > "id" vs. NSObject

Is there a difference between relating to an Object in a 'Polymorphic' way with the type id than as NSObject *? In what way is: NSString* aString = @"Hello"; id anObj = aString; different than: NSString* aString = @"Hello"; NSObject* anObj =…
Ohad Regev
  • 5,641
  • 12
  • 60
  • 84
9
votes
6 answers

unexpected '@' in program (Xcode)

All of the sudden Xcode is giving me an error "unexpected '@' in program" at the beginning of my object @interface. This is happening in a bunch of my objects that were previously working... Here's an example (with errors in comments) #import…
olynoise
  • 2,016
  • 2
  • 19
  • 32
1 2
3
59 60