在委托方法上携带自定义属性

时间:2014-06-26 23:59:37

标签: ios delegates

我目前正在提取一个“竞赛”对象列表,这些对象具有与之相关的位置信息。我想监视我附近的所有比赛。

       for(Contest *contest in contests) {

            NSMutableDictionary * fence1 = [NSMutableDictionary new];
            [fence1 setValue:contest.event.name forKey:@"identifier"];
            [fence1 setValue:[NSString stringWithFormat:@"%f", contest.location.latitude] forKey:@"latitude"];
            [fence1 setValue:[NSString stringWithFormat:@"%f", contest.location.longitude] forKey:@"longitude"];
            [fence1 setValue:@"1000" forKey:@"radius"];

            //the below method returns a CLRegion object
            CLRegion *region = [self dictToRegion:fence1];

            //adding custom contest object to region
            region.contest = contest;

            [self.locationManager requestStateForRegion:region];
        }

我还希望将自定义竞赛属性绑定到该区域(如上面的代码所示),但它似乎没有在委托方法didDetermineState中继承:

-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
switch (state) {
    case CLRegionStateUnknown:
        NSLog(@"Unknown state!");
        break;

    case CLRegionStateInside:
        NSLog(@"Inside state!");

        //this comes back as null
        NSLog(@"%@", region.contest.name);

        break;

    case CLRegionStateOutside:
        NSLog(@"Outside state!");
        break;

    default:
        break;
}
}

在delegate方法中,region.contest返回为(null)。有更好的方法吗?

附录(我的CLRegion类别): CLRegion + Contest.h:

#import <CoreLocation/CoreLocation.h>
#import "Event.h"
#import "Contest.h"

@interface CLRegion (Loopd)

@property (nonatomic, strong) Event *event;
@property (nonatomic, strong) Contest *contest;

@end

CLRegion + Contest.m:

#import "CLRegion+Loopd.h"
#import <objc/runtime.h>

static char defaultHashKey;

@implementation CLRegion (Loopd)

@dynamic event;
@dynamic contest;

-(Event* ) event {
    return objc_getAssociatedObject(self, &defaultHashKey);
}

- (void) setEvent:(Event *)event {
    objc_setAssociatedObject(self, &defaultHashKey, event, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(Contest* ) contest {
    return objc_getAssociatedObject(self, &defaultHashKey);
}

- (void) setContest:(Contest *)event {
    objc_setAssociatedObject(self, &defaultHashKey, event, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


@end

1 个答案:

答案 0 :(得分:1)

您的类别有2个问题。 1.您为这两个属性使用相同的密钥。 2. defaultHashKey初始化为0值。

objc_getAssociatedObject和objc_setAssociatedObject的工作方式大致类似于setValue:forKey:和getValueForKey:。因此,您需要提供将值绑定到对象的键。尝试类似的东西:

#import "CLRegion+Loopd.h"
#import <objc/runtime.h>

static char* kEventKey = "kEventKey";
static char* kContestKey = "kContestKey";

@implementation CLRegion (Loopd)

@dynamic event;
@dynamic contest;

-(Event* ) event {
    return objc_getAssociatedObject(self, kEventKey);
}

- (void) setEvent:(Event *)event {
    objc_setAssociatedObject(kEventKey, event, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(Contest* ) contest {
    return objc_getAssociatedObject(self, kContestKey);
}

- (void) setContest:(Contest *)event {
    objc_setAssociatedObject(self, kContestKey, event, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

这段代码应该可以工作,但是创建单独的对象以存储这种关联会更好(我不是为了这种目的而使用运行时的忠实粉丝)。

编辑: 您在for循环中多次竞争的分配值。因此,其中一个竞赛(最后一个竞赛)被分配给CLRegion对象。