1

我是 Objective-c 的新手,我正在尝试理解 Xcode。

现在,我在下面的代码中遇到了很大的麻烦:

AppDelegate.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate> {

    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextField *locationTitleField;
    IBOutlet MKMapView *worldView;
    IBOutlet CLLocationManager *locationManager;

}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;


@end

AppDelegate.m

    #import "AppDelegate.h"

    #import "ViewController.h"

    @implementation AppDelegate

    @synthesize window = window;
    @synthesize viewController = viewController;


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        locationManager = [[CLLocationManager alloc]init];
        [locationManager setDelegate:self];
        [locationManager setDistanceFilter:kCLDistanceFilterNone];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [worldView setShowsUserLocation:YES];


        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
   }

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
     // Yes, no code here, but here's the point, where I'm getting crashes, no matter if
     // there is some code in here, or not
    }
    @end

问题是,当我尝试启动应用程序时,一切都很好。但是“didUpdateUserLocation”真的让我发疯了。我已经打开了 Zombie Objects,现在 Xcode 告诉我:

[AppDelegate mapView:didUpdateUserLocation:]: message sent to deallocated instance 0xde1b700
(lldb) 

我已经(!)打开了新的 ARC 东西,但是我把它关掉了,仍然得到同样的错误。如您所见,我的代码中什至没有至少一个版本。

4

1 回答 1

0

您误解了错误消息。您在消息被解除分配或委托集无效AppDelegate后被发送。此方法中的内容无关紧要,重要的是应用程序委托不是有效对象。您没有说何时收到此错误并且您没有显示足够的代码来告诉您,但是您的设置不正确或退出时正确拆除。mapView:didUpdateUserLocation:MKMapViewworldView

为什么你被locationManager声明为一个IBOutlet然后由你分配/初始化?您在 nib 文件中为此对象所做的任何设置都将通过替换它而丢失,并且如果没有 ARC,原始文件将会泄漏。

于 2012-07-21T16:35:35.140 回答