2012-03-13 61 views
1

在Xcode 4.3.1,目標的iPad 5.1模擬器Objective-C和Xcode的奇怪的行爲

  • 創建單個視圖應用
  • 使用ARC,在視圖控制器不使用故事板

  • .H

    #import <UIKit/UIKit.h> 
    
    @interface ViewController : UIViewController 
    { 
         NSObject *anObject; 
         NSObject *anotherObject; 
    } 
    
    -(void) makeObjects; 
    
    @end 
    
  • 在ViewController.m添加

    -(void) makeObjects{ 
         anObject = [[NSObject alloc] init]; 
         anotherObject = [[NSObject alloc] init]; 
    
         int a = 1; 
    } 
    
  • 在AppDelegate.m添加一行

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
    { 
         self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
         // Override point for customization after application launch. 
         self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
         self.window.rootViewController = self.viewController; 
    
         [self.viewController makeObjects]; // ADD THIS LINE <-------- 
    
         [self.window makeKeyAndVisible]; 
         return YES; 
    } 
    
  • 在ViewController.m

    ,在

    anObject = [[NSObject alloc] init]; 
    
  • 運行

  • 步驟在斷點
  • 設置斷點anObject = 0x00000000,另一個對象被設置!
+0

我喜歡你所做的格式。讀起來非常有趣:) – deanWombourne 2012-03-13 17:23:56

+0

Lol Rob,這是我在Stackoverflow上的第一篇文章。我沒有格式化文本,只需複製並粘貼:) – 2012-03-14 10:58:06

+0

有一個編輯按鈕。 。 。 – deanWombourne 2012-03-14 12:13:29

回答

1

您使用的是LLDB調試器嗎?目前它在模擬器中不會給你正確的iVar值。切換回GDB,你會發現報告的正確值。我在這裏發現了這種行爲:UIViewController subclass can't assign instance variable

是的,我報告了一個錯誤。我收到了蘋果的迴應,聲明這是一個已知問題。

+0

謝謝Aaron,我在使用LLDB。 – 2012-03-14 10:54:50

+0

沒問題@AlessandroCalciati,你介意接受我的回答嗎?謝謝 :) – 2012-03-14 13:00:26