我知道这是一个愚蠢的问题,但我找不到解决方案。
这里是我的代码:
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