作者Fantasywind (Fantasywind)
看板MacDev
標題[問題] NSObject property NSMutableArray 傳值
時間Thu Aug 9 20:24:48 2012
哈囉大家好,近來開始自學Objective-C on iOS Programming
參考書籍是 Learning iOS Programming 2nd / O'Reilly
目前嘗是撰寫一個讀取並分析RSS/xml的功能,參照書中作法建議一獨立Class (NSObject)
專責網路連線(NSURLConnection)與分析作業(libxml)
我在SPNewsGather中宣告四個 NSMutableArray 作為分析後資料儲存之用:
並建立一個溝通用的Method
@property (weak, nonatomic) NSArray *titles;
@property (weak, nonatomic) NSMutableArray *links;
@property (weak, nonatomic) NSMutableArray *descriptions;
@property (weak, nonatomic) NSMutableArray *images;
- (void)rssGetter:(UIViewController *)controller;
而我的主體ViewController是tabbed table view
我先在SPAppDelegate 將SPNewsGather alloc/init 並 bind on viewControll:
UIViewController *viewController1 = [[SPFirstViewController alloc] initWithNibName:@"SPFirstViewController" bundle:nil];
SPSecondViewController *viewController2 = [[SPSecondViewController alloc] initWithNibName:@"SPSecondViewController" bundle:nil];
UIViewController *viewController3 = [[SPThirdViewController alloc] initWithNibName:@"SPThirdViewController" bundle:nil];
UIViewController *viewController4 = [[SPFourthViewController alloc] initWithNibName:@"SPFourthViewController" bundle:nil];
UIViewController *viewController5 = [[SPFifthViewController alloc] initWithNibName:@"SPFifthViewController" bundle:nil];
SPNewsGather *newsGather = [[SPNewsGather alloc] init];
viewController2.newsGather = newsGather;
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4, viewController5];
self.window.rootViewController = self.tabBarController;
於SPSecondViewController 中也有 import & property:
#import "SPNewsGather.h"
@property (strong, nonatomic) SPNewsGather *newsGather;
在其implement也可以正確抓到 self.newsGather (type: SPNewsGather Object)
也可以正確執行Method:
[self.newsGather rssGetter:self];
在落落長的前提下,問題來了
在不斷插入中斷點,我確定rssGather:有執行,其中所有的NSMutableArray也都可以
在SPNewsGather中看到值,最後也透過
[viewController.tableView reloadData];
來確保不會遇到不同資料問題,但是不管在reloadData之前或之後
SPSecondViewController中使用NSLog查看 self.newsGather.titles 的值都是(null)
本來我以為是連結上有問題,我有另外以同樣方法宣告一個
NSString *testString;
然後在SPnewsGather中改變他的值
然後我在SPSecondViewController 中 self.newsGather.testString
就可以看到正確的改變,也就是說只有NSMutableArray無法正確傳值
昨天測這個整夜沒睡,希望有高手可以幫個忙QQ
完整的程式碼可以在Github看到
https://github.com/fantasywind/Shockpaper
謝謝你花了時間看這篇落落長的問題 :)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.45.22.35
1F:→ kiii210:build不起來..囧 08/09 20:50
2F:推 offname:有什麼特別的原因要用 weak 嗎? 08/09 20:55
3F:→ Fantasywind:書上的Parser例子只有用weak我就沒有特別想過 @@ 08/09 21:04
4F:→ Fantasywind:真的是這個問題!! 改成strong全部都正常了.. 08/09 21:06
5F:→ Fantasywind:來研究一下這部分 感謝offname大 !! 08/09 21:06
6F:→ Fantasywind:對ARC的運作實在不是很懂 08/09 21:08
7F:→ darktt:其實對ARC還不懂的話可以先關閉它,使用舊方法來解決即可 08/09 22:00