如果我创建一个属性,例如:
@property(nonatomic, strong) NSString *RSSIvalue;
然后我合成它:
@synthesize RSSIvalue;
然后我可以将其定义为一种方法中的某些内容吗:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
RSSIvalue = [NSString stringWithFormat:@"%ld", (long)RSSI.integerValue];
}
然后使用我在另一个方法中定义的值:
- (IBAction)setNValueAt1meter:(id)sender
{
self.nValue1m.text = RSSIvalue;
}
最佳答案
可以,只要你的订单正确。
但是在我看来,RSSIvalue
应该存储为在演示期间格式化的数字:
@property(nonatomic, strong) NSNumber *RSSIvalue;
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
self.RSSIvalue = RSSI;
}
- (IBAction)setNValueAt1meter:(id)sender
{
self.nValue1m.text = [NSString stringWithFormat:@"%@", self.RSSIvalue];
// same as this, only more expensive:
// self.nValue1m.text = [self.RSSIvalue description];
}
注意self.RSSIvalue
的使用。您甚至可以将其设置为long
,这样更简单。
关于objective-c - 对象值在方法之间传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20132106/