Iphone Objective-C-将变量传递到函数时的错误
将var传递给另一个函数的最简单方法是什么Iphone Objective-C-将变量传递到函数时的错误,iphone,ios,objective-c,cordova,Iphone,Ios,Objective C,Cordova,将var传递给另一个函数的最简单方法是什么 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"%@", started); } 我试过: 定义了一个全局变量: extern NSString *started;
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", started);
}
我试过:
定义了一个全局变量:
extern NSString *started;
当我直接设置NSString并将其传递到另一个函数时,它工作得很好:
-(void) startTracking:(CDVInvokedUrlCommand*)command {
started = @"testing";
}
但它不起作用:
-(void) startTracking:(CDVInvokedUrlCommand*)command {
NSString* myarg = [command.arguments objectAtIndex:0]; // http://docs.phonegap.com/en/2.5.0/guide_plugin-development_ios_index.md.html#Developing%20a%20Plugin%20on%20iOS_writing_an_ios_cordova_plugin
started = myarg;
}
(我是objective-C初学者,不太懂)
编辑:似乎只有当我将应用程序放到后台时它才会崩溃。根据您是否使用ARC,您必须保留对象。您可能希望在类上使用属性: 在标题中:
@property(strong) NSString *started;
在执行中:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", self.started);
}
-(void) startTracking:(CDVInvokedUrlCommand*)command {
self.started = @"testing";
}
-(void) startTracking:(CDVInvokedUrlCommand*)command {
NSString* myarg = [command.arguments objectAtIndex:0];
self.started = myarg;
}
伙计,看来你想追踪你开始接收位置信息的日期 这样做怎么样:
// Your .h file
@interface MyClass <CLLocationManagerDelegate>
{
BOOL hasStartedUpdatingLocation;
NSDate *startDate;
CLLocationManager *locationManager;
}
...
// Your .m file
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// ---------------------------------------------------------------
// if has NOT started updating location, record start date
// otherwise, do not execute this if statement
// ---------------------------------------------------------------
if(!hasStartedUpdatingLocation)
{
hasStartedUpdatingLocation = YES;
// this if statement should only execute once
startDate = [NSDate date]; // get the current date and time
}
else
{
// do something else
}
}
//您的.h文件
@接口MyClass
{
BOOL-hasStartedUpdatingLocation;
NSDate*起始日期;
CLLocationManager*locationManager;
}
...
//你的.m文件
-(无效)locationManager:(CLLocationManager*)经理
DidUpdateLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation
{
// ---------------------------------------------------------------
//如果尚未开始更新位置,请记录开始日期
//否则,不要执行此if语句
// ---------------------------------------------------------------
如果(!hasstartedUpdateGlocation)
{
HassStartedUpdateGlocation=是;
//这个if语句应该只执行一次
startDate=[NSDate日期];//获取当前日期和时间
}
其他的
{
//做点别的
}
}
通过“[command.arguments objectAtIndex:0];”获取myarg中的命令值,您试图做什么?@PrakashDesai,这是从PhoneGap插件获取参数。重要的细微差别:这些不是“函数”,而是“方法”。方法与函数类似,不同之处在于它们与对象绑定并可以访问其状态(任何变量)。重写:函数可以访问传递的变量(参数)和全局变量(尽量避免/最小化最后一个变量);方法可以访问传递的所有这些变量及其对象的变量。@Qooe您得到的是什么error
?好的,您还可以删除BOOL并将if语句更改为类似if(startDate==nil){…}
:-)谢谢@Zhang,想将用户令牌传递到DidUpdateLocation什么是用户令牌?这是永远不会改变的吗?您可以将其存储在NSUserDefaults中,然后将其取回。否则,如果只是临时的,您可以在视图控制器头文件中声明一个实例变量,并将用户令牌存储在那里,这样就可以在整个视图控制器中访问它。