从^ block访问@property

时间:2013-04-12 18:52:02

标签: ios objective-c objective-c-blocks

当我尝试对我在StreamScreen UIViewController类中声明的@property进行更改时,我收到了(lldb)错误。

我正在尝试更改asyncRequest成功块中的feedList对象。 asyncRequest方法是我导入的类方法,是我的类的一部分,名为NSURLConnection-block。

因此...

我正在尝试更改一个NSMutableArray(feedList),它是StreamScreen中一个块内的StreamScreen的一部分,该块由另一个类的类方法(asyncRequest)调用,但是我收到错误。

在调试导航器中,我得到了 - error: address doesn't contain a section that points to a section in a object file

以下是我的部分代码......

这是我声明feedList @property(StreamScreen.h)

的地方
@property (strong, nonatomic) NSMutableArray *feedList;

以下代码是(StreamScreen.m)中带有asyncRequest类方法的getStream方法

- (void)getStream {
    NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
    NSString *urlString = [NSString stringWithFormat: @"http://198.199.71.169/getFB.php?at=%@", fbAccessToken];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [NSURLConnection asyncRequest:request
                          success:^(NSData *data, NSURLResponse *response) {
                              NSLog(@"%@", feedList);
                          }
                          failure:^(NSData *data, NSError *error) {
                              NSLog(@"Error! %@",[error localizedDescription]);
                          }];
}

我实例化feedList NSMutableArray并在我的viewDidLoad方法中调用getStream方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    feedList = [[NSMutableArray alloc] initWithObjects:@"zero", @"one", @"two", nil];
    [self getStream];    
}

修改

这是NSURLConnection-block.m

#import "NSURLConnection-block.h"

@implementation NSURLConnection (block)

#pragma mark API
+ (void)asyncRequest:(NSURLRequest *)request success:(void(^)(NSData *,NSURLResponse *))successBlock_ failure:(void(^)(NSData *,NSError *))failureBlock_
{
    [NSThread detachNewThreadSelector:@selector(backgroundSync:) toTarget:[NSURLConnection class]
                           withObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                       request,@"request",
                                       successBlock_,@"success",
                                       failureBlock_,@"failure",
                                       nil]];
}

#pragma mark Private
+ (void)backgroundSync:(NSDictionary *)dictionary
{
    //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    void(^success)(NSData *,NSURLResponse *) = [dictionary objectForKey:@"success"];
    void(^failure)(NSData *,NSError *) = [dictionary objectForKey:@"failure"];
    NSURLRequest *request = [dictionary objectForKey:@"request"];
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(error)
    {
        failure(data,error);
    }
    else
    {
        success(data,response);
    }
    //[pool release];
}


@end

这是NSURLConnection-block.h

#import <Foundation/Foundation.h>

@interface NSURLConnection (block)
#pragma mark Class API Extensions
+ (void)asyncRequest:(NSURLRequest *)request success:(void(^)(NSData *,NSURLResponse *))successBlock_ failure:(void(^)(NSData *,NSError *))failureBlock_;
@end

我希望有人可以帮助我。我真的很感激! :)

3 个答案:

答案 0 :(得分:1)

您是否自己将feedListfeedList ivar合成?否则请尝试_feedList,这是默认的合成变量。

答案 1 :(得分:0)

如果编译器没有抱怨,你可能无法得到你期望的结果。您在制作feedList时冻结asyncRequest的值。

尝试

[self feedList]

或者更有用的是,response显然在块

的范围内

答案 2 :(得分:0)

可能由于多线程环境而发生错误:

当你把它放在成功区块时会发生什么?:

dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"%@",self.feedList);
});
相关问题