由于我无法在Objective-C中的类别中创建合成属性,因此我不知道如何优化以下代码:
@interface MyClass (Variant)
@property (nonatomic, strong) NSString *test;
@end
@implementation MyClass (Variant)
@dynamic test;
- (NSString *)test {
NSString *res;
//do a lot of stuff
return res;
}
@end
Run Code Online (Sandbox Code Playgroud)
该测试方法被调用运行时多次,我做了很多的东西来计算结果.通常使用合成属性我会在第一次调用方法时将值存储在IVar _test中,并且下次只返回此IVar.我该如何优化上面的代码?
我创建了一个UIImageView允许异步加载图像的类别:
@implementation UIImageView (ImageLoader)
- (void) asyncLoadImageIntoView:(NSURL *)imageURL withLoadingIndicator:(BOOL)isLoadingIndicator {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:imageURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData %@", data);
(void)[self.image initWithData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//[textView setString:@"Unable to fetch data"];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received image data");
}
@end
Run Code Online (Sandbox Code Playgroud)
我的问题是,即使connection:didReceiveData:调用该方法并将数据打印到控制台,[self.image initWithData:data]似乎也没有将图像加载到视图中.这里有什么问题?