我知道這是一個愚蠢的問題,但我找不到解決方案。
這裏是我的代碼:
ConnectionService.h
試圖從異步調用傳輸數據到另一個類
#import "LaunchScreenViewController.h"
@interface ConnectionService : NSObject
-(void)getsFeaturedProducts;
@end
ConnectionService.m
-(void)getsFeaturedProducts {
NSString *urlString = [NSString stringWithFormat:@"my-url",[[AppDelegate instance] getUrl]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLSessionDataTask *getData = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData* data, NSURLResponse *response, NSError* error){
NSString* rawJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *value = [rawJson JSONValue];
_featuredProducts = [[NSDictionary alloc]initWithDictionary:value];
NSLog(@"Featured products: %@", _featuredProducts);//not empty
LaunchScreenViewController *lsvc = [[LaunchScreenViewController alloc]init];
lsvc.featuredProducts = self.featuredProducts;
NSLog(@"Featured products: %@", lsvc.featuredProducts);//not empty
}];
[getData resume];
}
LaunchScreenViewController.h
#import "ConnectionService.h"
@interface LaunchScreenViewController : UIViewController
@property(nonatomic,strong) NSDictionary *featuredProducts;
@end
LaunchScreenViewController.m
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
ConnectionService *connectionService = [[ConnectionService alloc]init];
[connectionService refreshProducts];
self.featuredProducts = [[NSDictionary alloc]init];
NSLog(@"featuredProducts: %@", self.featuredProducts);//empty
NSArray *keys = [[NSArray alloc]initWithArray:[self.featuredProducts allKeys]];
NSLog(@"All featured product keys: %@", keys);
}
我做錯了什麼?
P.S.編程objective-c少於一個月,所以是的...謝謝你的-rep。
'[[洗衣機。 chScreenViewController alloc] init]'創建一個新的對象。然後你更新它的屬性,但是你永遠不會呈現它,所以這個對象的視圖永遠不會出現。如果您已在屏幕上顯示'LaunchScreenViewController',則需要使用它。 –
這是我第一次建議,但它沒有奏效。 – Ookey