0
我有2個視圖控制器向Web服務發送請求。 收到數據後,數據將保存到「文檔」文件夾中的文件中。文件路徑被其他視圖控制器修改
這些是2 VC:
Live_VC:
#import "FV_Live_ViewController.h"
@interface FV_Live_ViewController()
@end
@implementation FV_Live_ViewController
NSArray *paths;
NSString *documentsDirectory;
NSString *path;
- (void)viewDidLoad {
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths firstObject];
[self sendRequest]; // first request of data ("Live" data)
}
- (void)sendRequest {
// other code
// request of "Live" data
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
if (success) {
// other code
NSString *filename = [NSString stringWithFormat:@"months.plist"];
path = [documentsDirectory stringByAppendingPathComponent:filename]; // path = "...\months.plist"
// second request of data (monthly data) if months.plist doesn't exists
if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {
[self sendMonthRequest];
}
}
}];
}
- (void)sendMonthRequest {
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
if (success) {
// other code
[monthlyArray writeToFile: path atomically:YES]; // path should be "...\months.plist" while it is "...\yesterday.plist"
}
}];
}
@end
Today_VC:
#import "FV_Today_ViewController.h"
@interface FV_Today_ViewController()
@end
@implementation FV_Today_ViewController
NSArray *paths;
NSString *documentsDirectory;
NSString *path;
- (void)viewDidLoad {
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths firstObject];
[self sendRequest]; // first call of "sendRequest" to get today data
}
- (void)sendRequest {
// other code
// request of "Today" data
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
if (success) {
// other code
NSString *filename = [NSString stringWithFormat:@"today.plist"];
path = [documentsDirectory stringByAppendingPathComponent:filename]; // path = "...\today.plist"
[dataDictionary writeToFile: path atomically:YES]; // save data to today.plist
// second call of "sendRequest" to get yesterday data only if yesterday.plist doesn't already exists)
filename = [NSString stringWithFormat:@"yesterday.plist"];
path = [documentsDirectory stringByAppendingPathComponent:filename]; // path = "...\yesterday.plist"
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
[self sendRequest];
}
}];
}
@end
在每個VC我使用一個NSString( 「路徑」)來存儲路徑但問題是,在「sendMonthRequest」方法(Live_VC)中,路徑的值是在其他VC(Today_VC)中設置的值。 怎麼可能?第一個VC中的NSString的值如何由第二個VC改變?
感謝, 科拉多
謝謝!我不知道通過將var放在實現文件中而不包含var變成全局的。我認爲在實現中聲明的var是私有的,並且要共享我需要在接口中定義它的變量,並在需要訪問它的文件中導入接口。這很容易把它放在.m沒有括號...我錯過了什麼?你會善意澄清它嗎?謝謝你,Corrado – Corrado 2014-09-06 14:03:55
@Corrado的確,如果你在實現文件中添加ivars,它們是「私有的」(objective-c中沒有任何真正的私有),如果你想讓其他類能夠訪問ivar,你應該把它放在界面中;你對此的理解是正確的。但是,如果你創建一個全局變量,那麼你放入哪個文件並不重要,它可以用於任何也聲明該變量的類(就像你在兩個類中做的那樣)。你需要注意那些花括號。 – rdelmar 2014-09-06 15:24:38
我會的!再次感謝 ! – Corrado 2014-09-06 16:49:48