Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble using a custom NSObject class to simplify repetitive code in iPhone app
    primarykey
    data
    text
    <p>I am trying to store a REST connection model in a single object so that I don't keep having to use it over and over again. Here is the model I created:</p> <pre class="lang-c prettyprint-override"><code>//RestModel.h #import "ASIHTTPRequest.h" @interface RestModel : NSObject{ NSString* _baseUrl; NSString* _modelUrl; } @property (nonatomic, retain) NSString* modelUrl; - (id)initWithModel:(NSString*)model; - (NSDictionary*)getById:(NSInteger*)ident; - (NSDictionary*)getAll; @end //RestModel.m #import "RestModel.h" @implementation RestModel @synthesize modelUrl = _modelUrl; - (id)init { self = [super init]; if (self) { _baseUrl = @"http://myRESTurl.com"; } return self; } - (id)initWithModel:(NSString*)model { self = [super init]; if (self) { _baseUrl = @"http://myRESTurl.com"; self.modelUrl = [NSString stringWithFormat:@"%@/%@/", _baseUrl, model]; } return self; } - (NSDictionary*)HTTPRequest:(NSURL*)url { ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request startSynchronous]; NSError *error = [request error]; if(!error){ NSData *responseData = [request responseData]; NSString *errorDesc = nil; NSPropertyListFormat format; [error release]; [request release]; return (NSDictionary*)[NSPropertyListSerialization propertyListFromData:responseData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&amp;format errorDescription:&amp;errorDesc]; }else{ NSLog(@"%@", error); return nil; } } - (NSDictionary*)getById:(NSInteger*)ident { NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.modelUrl, ident]]; return [self HTTPRequest:url]; } - (NSDictionary*)getAll { NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.modelUrl]]; return [self HTTPRequest:url]; } - (void)dealloc { [_modelUrl release]; // [_responseData release]; // [_responseDict release]; [super dealloc]; } @end </code></pre> <p>Edit: Now it isn't crashing, but my local NSDictionary has a count of 0. I'm calling the following in -viewDidLoad in my controller:</p> <pre class="lang-c prettyprint-override"><code>RestModel* rm = [[RestModel alloc] initWithModel:@"user"]; self.dict = [rm getAll]; [rm release]; </code></pre> <p>I planted NSLog of [self.dict count] throughout the controller. It is always 0. The RestModel rm is called, the functions are called (again more NSLogs), but no data. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload