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
19
votes
5 answers

iOS JSON serialization for NSObject-based classes

I'd like to JSON-serialize my own custom classes. I'm working in Objective-C / iOS5. I'd like something to do the following: Person* person = [self getPerson ]; // Any custom object, NOT based on NSDictionary NSString* jsonRepresentation =…
Journeyman
  • 10,011
  • 16
  • 81
  • 129
18
votes
3 answers

NSDictionary vs. custom object

The question is quite simple: When I create a new API or Service Class should I create a custom class for the objects that are being passed around or should I just stick to an NSDictionary which simply holds the data in a key-value-style…
Besi
  • 22,579
  • 24
  • 131
  • 223
18
votes
5 answers

iOS: create an object class with Swift

I created this class for my object City class City: NSObject { var _name:String = "" var name:String { get { return _name } set (newVal) { _name = newVal } } } then when I create…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
17
votes
6 answers

how to forbid the basic init method in a NSObject

I want to force user to use my own init method (for example -(id)initWithString:(NSString*)foo;) and not the basic [[myObject alloc]init];. how can I do that?
Anthony
  • 2,801
  • 3
  • 30
  • 49
17
votes
4 answers

How to call a method a.s.a.p. but at earliest in the next run loop iteration?

I need a save way to say: "iOS, I want this method to be executed a.s.a.p., but NOT in THIS run loop iteration. At the earliest in the next, but please not in this one. Thank you." Right now I am always doing it like this: [self…
openfrog
  • 40,201
  • 65
  • 225
  • 373
17
votes
3 answers

Monotouch: convert an Object to NSObject

How is it possible to convert an Object instance to NSObject one? I've created a NSDictionary from NSDictionary.FromObjectAndKey(); This method wants an NSObject but I have custom object to pass in: int key = 2341; var val = new…
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
16
votes
5 answers

How do copy for UILabel?

I have IBOutlet UILabel *label; and I want to do this UILabel *label = [titleLabel copy]; label.text = @"Clone"; titleLabel.text = @"Original"; NSLog(@"label : %@, title : %@",label.text,titleLabel.text); and this throw exception * Terminating…
Igor Bidiniuc
  • 1,540
  • 1
  • 16
  • 27
15
votes
1 answer

Calling super.init() in initializer of NSObject subclass in Swift

I'm building an iOS app in Swift and drawing on the Lister sample project Apple provides. Lister uses two model objects: List and ListItem. I found that both of them do not call super.init() in their initializers even though they subclass…
sdduursma
  • 2,651
  • 3
  • 16
  • 16
14
votes
3 answers

Is there a reverse "setValuesForKeysWithDictionary" - a makeDictionaryWithObjectProperties?

I parse some JSON from a web service, this gives me an NSDictionary, I use this dictionary to populated properties on a valueEntity of type NSObject by [myObject setValuesForKeysWithDictionary:JSONDict]; (myObject has the same property names and…
RickiG
  • 11,380
  • 13
  • 81
  • 120
14
votes
6 answers

Swift compile error when subclassing NSObject and using generics

The following Swift code generates a compile error at build time: import Foundation class Wrapper : NSObject { let obj : T init(x : T) { self.obj = x } } Am I doing something wrong or is this a compiler bug? If so, what…
hpique
  • 119,096
  • 131
  • 338
  • 476
13
votes
3 answers

Do I need to call [super init] or [super initWithCoder], etc for NSObject

Typically when I subclass from a UI class I will call the superclass initializer of interest. However, I'm not sure of the implementation details of NSObject, and it seems like there's not much going on in terms of member vars, so I wonder: do I…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
13
votes
1 answer

reimplementing NSObject from scratch

When I was reading about the new 4.0.2 iOS update I wanted to know what hackers do or try doing with a buffer overflow, which after some wikipedia'ing got me interested in playing with malloc and thus creating my own "NSObject". I am not actually…
Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
13
votes
2 answers

Why do we call doesNotRecognizeSelector: method?

I am working with socket programming.I just wanted to clear a doubt related with a code i downloaded from -mobileorchard.com - Chatty. While R&D , I saw a function call in ChatRoomViewController.m file [chatRoom broadcastChatMessage:input.text…
HarshIT
  • 4,583
  • 2
  • 30
  • 60
12
votes
2 answers

Why do Objective-c protocols adopt other protocols?

I've seen Objective-c protocols defined in the following way: @protocol MyProtocol // ... @end Why do protocols adopt other protocols? I'm especially curious why a protocol would adopt the NSObject protocol.
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
12
votes
2 answers

Using a typedef enum in my object Class

I have a People class which holds various bits of into about a person. I would like to be able to identify what kind of person this is, so I thought I would try using a typedef enum for this since I have seen it done before and it seems like the…
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
1
2
3
59 60