IPhone SDK / Objective C Syntax Question
Everyone
I was looking at the sample project http://iphoneonrails.com/ and I saw that they took the NSObject class and added methods for SOAP calls to it.
Their sample project can be downloaded here http://iphoneonrails.com/downloads/objective_resource-1.01.zip .
My question stems from my lack of knowledge of the following syntax as I have not seen it in an iPhone project yet.
There is a header file NSObject + ObjectiveResource.h where they declare and modify NSObject to have additional methods for this project. Is "+ ObjectiveResource.h" in the name a special syntax or is it just a developer naming convention.
Finally, inside the NSObject class, we have the following
#import <Foundation/Foundation.h>
@interface NSObject (ObjectiveResource)
// Response Formats
typedef enum {
XmlResponse = 0,
JSONResponse,
} ORSResponseFormat;
// Resource configuration
+ (NSString *)getRemoteSite;
+ (void)setRemoteSite:(NSString*)siteURL;
+ (NSString *)getRemoteUser;
+ (void)setRemoteUser:(NSString *)user;
+ (NSString *)getRemotePassword;
+ (void)setRemotePassword:(NSString *)password;
+ (SEL)getRemoteParseDataMethod;
+ (void)setRemoteParseDataMethod:(SEL)parseMethod;
+ (SEL) getRemoteSerializeMethod;
+ (void) setRemoteSerializeMethod:(SEL)serializeMethod;
+ (NSString *)getRemoteProtocolExtension;
+ (void)setRemoteProtocolExtension:(NSString *)protocolExtension;
+ (void)setRemoteResponseType:(ORSResponseFormat) format;
+ (ORSResponseFormat)getRemoteResponseType;
// Finders
+ (NSArray *)findAllRemote;
+ (NSArray *)findAllRemoteWithResponse:(NSError **)aError;
+ (id)findRemote:(NSString *)elementId;
+ (id)findRemote:(NSString *)elementId withResponse:(NSError **)aError;
// URL construction accessors
+ (NSString *)getRemoteElementName;
+ (NSString *)getRemoteCollectionName;
+ (NSString *)getRemoteElementPath:(NSString *)elementId;
+ (NSString *)getRemoteCollectionPath;
+ (NSString *)getRemoteCollectionPathWithParameters:(NSDictionary *)parameters;
+ (NSString *)populateRemotePath:(NSString *)path withParameters:(NSDictionary *)parameters;
// Instance-specific methods
- (id)getRemoteId;
- (void)setRemoteId:(id)orsId;
- (NSString *)getRemoteClassIdName;
- (BOOL)createRemote;
- (BOOL)createRemoteWithResponse:(NSError **)aError;
- (BOOL)createRemoteWithParameters:(NSDictionary *)parameters;
- (BOOL)createRemoteWithParameters:(NSDictionary *)parameters andResponse:(NSError **)aError;
- (BOOL)destroyRemote;
- (BOOL)destroyRemoteWithResponse:(NSError **)aError;
- (BOOL)updateRemote;
- (BOOL)updateRemoteWithResponse:(NSError **)aError;
- (BOOL)saveRemote;
- (BOOL)saveRemoteWithResponse:(NSError **)aError;
- (BOOL)createRemoteAtPath:(NSString *)path withResponse:(NSError **)aError;
- (BOOL)updateRemoteAtPath:(NSString *)path withResponse:(NSError **)aError;
- (BOOL)destroyRemoteAtPath:(NSString *)path withResponse:(NSError **)aError;
// Instance helpers for getting at commonly used class-level values
- (NSString *)getRemoteCollectionPath;
- (NSString *)convertToRemoteExpectedType;
//Equality test for remote enabled objects based on class name and remote id
- (BOOL)isEqualToRemote:(id)anObject;
- (NSUInteger)hashForRemote;
@end
What is the ObjectiveResource object in tool () for NSObject? What does Xcode and the compiler say about what's going on ..?
After that everything looks fine to me as they have different static and instance methods. I know that with this, all custom classes that inherit from NSObject now have all the additional methods for that project.
My question is what does the parenthesis after NSObject do. This is a link to a header file, or it lets the compiler know that this class is in the path.
Thanks again and my apologies ahead of time if this is a stupid question but just an attempt to find out what I am missing.
a source to share
This is Category
. It allows you to extend an existing class. Instead
MyClass : MySuperclass
it is declared via:
MyClass (MyCategoryName)
The filename is just a convention MyClass+MyCategory.h
. The file name doesn't matter at all, but it makes it easy to tell that the file contains a category of methods that extends a specific class.
Note that you can inject new methods into categories, but you cannot add instance variables. And to use a category elsewhere in your codebase, all you have to do is import the category header and all of its methods will be available to the objects that the category applies to.
a source to share
This is called a "category" in Objective-C. For more information, see the Categories and Extensions section of the Objective-C chapter.
+ObjectiveResource.h
the thing is just the usual naming convention for files containing categories. It doesn't make any difference to the compiler.
a source to share
The first two answers are correct - it is Category, which is a way to extend the functionality of an existing class.
This is a really powerful feature in Objective-C, but it can be overused too. It can be dangerous to put Categories on low-level classes like NSObject if you don't know exactly what the implications are. For example, if you add methods to NSObject, you are essentially adding methods to any class that inherits from NSObject, which is pretty much everything. Note that you can only use category methods if you include a title, but in some cases it can give unexpected results.
a source to share