开发者

Class return NSMutabledata

开发者 https://www.devze.com 2023-03-10 04:30 出处:网络
I want to write a class which return NSMutableData, i have this code but couldn\'t manage how it return self. any help would be great.

I want to write a class which return NSMutableData, i have this code but couldn't manage how it return self. any help would be great.

@interface ITumblr_QueryTumblr : NSMutableData {

    NSURLConnection* connection;
    NSMutableData* data;    

}



-(void)loadImageFromURL:(NSURL*)url {

    if (connection!=nil) { [connection release]; }
    if (data!=nil) { [data release]; }
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

-(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {

    if (data==nil) {

        data = [[NSMutableData alloc] initWithCapacity:2048];

    }

   开发者_运维技巧 [data appendData:incrementalData];

}

-(void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    [connection release];
    connection=nil;

    [data release];
    data=nil;

}


A class doesn't return anything, though a method can. To have a method that returns data, use

-(NSMutableData *)returnSomeData {
  return data;
}

Alternately, you can just call the property .data from an instance of your class.


assuming that at your url you have a .plist file containing a dictionary, you will have to modify your connectionDidFinishLoading to something like this :

(void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    [connection release]; connection=nil;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0];

     NSString *myFilePath = [documentsDirectoryPathstringByAppendingPathComponent:@"myfile.plist"];

     [data writeToFile:myFilePath atomicaly:YES];

    [data release]; data=nil;                           

     return [NSMutableDictionary dictionaryWithContentsOfFile:myFilePath];
}

but this is a hard method, you should use :

 (id)dictionaryWithContentsOfURL:(NSURL *)aURL

because it saves you from the trouble

0

精彩评论

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