Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 试图从iOS中的另一个类中提取属性_Iphone_Objective C_Ios_Ios5_Gps - Fatal编程技术网

Iphone 试图从iOS中的另一个类中提取属性

Iphone 试图从iOS中的另一个类中提取属性,iphone,objective-c,ios,ios5,gps,Iphone,Objective C,Ios,Ios5,Gps,我对iOS开发完全陌生,所以我可能做得不对,但我有一个类,我用它来获取坐标gps数据,作为一个通用类,我可以在很多应用程序中重用。我的问题是如何将来自gps的数据正确地显示在其他应用程序中 以下是我的GPS类头文件: #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface LocationAwareness : NSObject <CLLocationMa

我对iOS开发完全陌生,所以我可能做得不对,但我有一个类,我用它来获取坐标gps数据,作为一个通用类,我可以在很多应用程序中重用。我的问题是如何将来自gps的数据正确地显示在其他应用程序中

以下是我的GPS类头文件:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface LocationAwareness : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property(copy) NSString *longitude;
@property(copy) NSString *latitude;

@end
#导入
#进口
@接口位置感知:NSObject{
CLLocationManager*locationManager;
}
@属性(副本)NSString*经度;
@属性(副本)NSString*纬度;
@结束
以下是实施方案:

#import "LocationAwareness.h"

@implementation LocationAwareness
@synthesize longitude;
@synthesize latitude;


- (id)init {

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Stops updating location if data has been updated within 10 minutes
    if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 600) {

        [locationManager stopUpdatingLocation];
        float latitudedata = newLocation.coordinate.latitude;
        latitude = [NSString stringWithFormat:@"%f", latitudedata];

        float logitudedata = newLocation.coordinate.longitude;
        longitude = [NSString stringWithFormat:@"%f", logitudedata];

    }
}

@end
#导入“LocationAwareness.h”
@实现位置感知
@综合经度;
@综合纬度;
-(id)init{
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
locationManager.distanceFilter=KCLDistanceFilterOne;//无论何时移动
locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;//100米
[locationManager startUpdatingLocation];
回归自我;
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
//如果数据在10分钟内更新,则停止更新位置
如果(abs([newLocation.timestamp timeintervalcescedate:[NSDate date]])<600){
[locationManager停止更新位置];
浮动纬度数据=newLocation.coordinate.latitude;
纬度=[NSString stringWithFormat:@“%f”,纬度数据];
float logitudedata=newLocation.coordinate.longitude;
经度=[NSString stringWithFormat:@“%f”,logitudedata];
}
}
@结束

现在我似乎找不到任何地方可以告诉我如何在另一个项目中获得纬度或经度属性。我导入了标题,并尝试将LocationAwareness.latitude存储到一个我可以使用的变量中,但我存储它的所有内容最终都是空的。当我开始我的主类和aloc初始化一个locationawareness对象时,gps会启动,所以我认为它可以工作,但我似乎不太了解它是如何工作的,以使一切井然有序。我在网上搜索了几个小时。有人知道我做错了什么吗?

好吧,这可能是也可能不是导致问题的原因(很有可能),但一个主要问题是您的init方法

首先应该是:

self = [super init];
if (self) {
    // Do your initializing as you did above.
}
return self;
编辑:
我把你的代码和我的更新一起添加到一个项目中,效果很好。 要使用此功能,您应该执行以下操作:

LocationAwareness *loc = [[LocationAwareness alloc] init];

// Give it some time to start updating the current location and then
// in a different function:
NSLog(@"%@", loc.latitude);
编辑2
无论您在哪里使用它,您都需要声明一个存储它的属性,以便可以创建一次并多次引用它。为此,请使用以下代码:

在要使用此对象的对象的标题中,将其与其他属性一起添加:

@property (nonatomic, assign) LocationAwareness *location;
然后,在实现文件(.m文件)的顶部,您应该看到其他@synthesis行,添加以下行:

@synthesize location;
然后,按照上面的示例创建要使用的实际位置实例:

self.location = [[LocationAwareness alloc] init];
现在给它一些时间来找出你的位置,并开始提供更新。然后可以按如下方式打印位置:

NSLog(@"%@", self.location.latitude);

如果看不到您是如何实现所有这些,就很难知道发生了什么。作为实际检查,也许可以尝试在
locationManager:didUpdateToLocation:
函数中放入一条log语句,这样您就可以确保有值可获取。换句话说:`NSLog(@“经度:%@”,经度)`也许我应该说我想知道一个简单的导入和打印到屏幕会是什么样子。我知道如何使用IBOulets和所有这些,但我真的不知道如何使属性从一个类交叉到另一个类。是的,我还应该提到我不知道init是否是在那里正确执行的事情?是的,但是如果你不调用super,你继承的所有类(包括NSObject)都不会被初始化。这将导致各种疯狂的错误!像这样(id)init{self=[super init];if(self){locationManager=[[CLLocationManager alloc]init];locationManager.delegate=self;locationManager.distanceFilter=kcldistancefilterne;locationManager.desiredAccuracy=kclocationaccuracityhundredmeters;//100米[locationManager startUpdatingLocation];}return self;}我所遵循的教程是针对viewcontroller的,但我想更改它。我在viewdidload函数中的一个全新项目中尝试了您的第二个代码块,注意nslog:2012-04-03 20:08:56.794 ClassTest[58569:c07](null)有什么想法吗?也许我搞错了?