开发者

category naming convention in objC

开发者 https://www.devze.com 2023-03-05 04:25 出处:网络
studying some sample code from an iOS programming course (cs193p fall2010) i came across the sbjson framework which extends NSObject etc. by a category named SBJSON.

studying some sample code from an iOS programming course (cs193p fall2010) i came across the sbjson framework which extends NSObject etc. by a category named SBJSON. but in the +header "NSObject+SBJSON.h" it reads as:

@interface NSObject (NSObject_SBJSON)

where does the magic mapping of NSObject_SBJSON to SBJSON come from? i noticed xcode acceptin开发者_开发技巧g any string before the underline !?

but nowhere found a hint on that.

thanx klaus


There isn't really a mapping, per se. An Objective-C category is used to add additional methods to an existing class, without needing direct access to that class's corresponding implementation file/details. So when you declare something like:

@interface NSObject (NSObject_SBJSON)

You are saying that your category adds methods to the NSObject class (and any class derived from it). The "NSObject_SBJSON" part in parenthesis is not really significant, and can be anything you like (so long as it doesn't collide with the name of anyone else's category for that object type). It does not even need to include "NSObject", so having @interface NSObject (SBJSON) would be equally valid.


Categories are not named in the sense that your code will refer to them by name like a Protocol. The category name in this case is Xcode's (poor) translation of the file name NSObject+SBJSON.h to a category name; for some reason Xcode or Objective-C's compiler doesn't want the + in the category name.

The only time the category name is used is to match interface and implementations.


The NSObject+SBJSON.h has been changed/solved in Xcode 4.2

0

精彩评论

暂无评论...
验证码 换一张
取 消