【发布时间】:2015-03-09 21:03:45
【问题描述】:
谁能告诉我这里哪里出错了。 我创建了一个名为 BeaconData 的 NSobject。头文件为:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface BeaconData : NSObject
@property (nonatomic, strong) NSMutableArray * jsonArray;
@property (nonatomic, retain) NSString * bMajor;
@property (nonatomic, retain) NSString * bMinor;
@property (nonatomic, retain) NSString * bUUID;
-(void) getData;
@end
那么实现文件是:
#import "BeaconData.h"
#define getDataURL @"http://www.eventav.biz/websitebeacons/library/json/files/beacons.txt"
@implementation BeaconData
@synthesize jsonArray, bUUID, bMajor, bMinor;
//Retrieve data
-(void) getData
{
extern NSString * bUUID;
extern NSString * bMajor;
extern NSString * bMinor;
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//Loop through Json Array
for (int i = 0; i < jsonArray.count; i++)
{
bUUID = [[jsonArray objectAtIndex:i] objectForKey:@"i_uuid"];
bMajor = [[jsonArray objectAtIndex:i] objectForKey:@"i_major"];
bMinor = [[jsonArray objectAtIndex:i] objectForKey:@"i_minor"];
}
}
@end
接下来我尝试在主 viewController.m 文件中调用全局变量 bMajor 并将其打印出来 - 只是看看它是否有效,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
extern NSString * bMajor;
NSInteger beaconMajorInt = [bMajor integerValue];
NSLog (@"Beacon bMajor is %li", (long)beaconMajorInt);
但我得到的只是以下错误:
架构 x86_64 的未定义符号: “_bMajor”,引用自: -ViewController.o 中的[ViewController viewDidLoad] ld:未找到架构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
【问题讨论】:
-
它不是全球性的,你为什么认为它是/希望它是?
-
为什么不是全球性的?就是那个问题。如果我只是在没有数组的情况下直接初始化变量 - NSString * bMajor = @"47008";像这样 - 然后它就可以了。
-
它不是一个全局变量。它是一个属性,其值特定于
BeaconData对象的给定实例。 -
好的 - 那么我如何让它全球化。正如我所说,这是一个有效的问题。如果我不尝试通过外部数据源(即使用数组/字典)对其进行初始化,并将其初始化为 NSString * bMajor = @"47008";那么它工作正常吗??
-
@Mile 只需在实现部分上方声明即可。
标签: objective-c global-variables extern