我是 iOS 编程的新手,我对单例类是什么以及为什么使用它非常感兴趣。我找到了一些信息,但它是模糊的。特别是我想把它应用到真实的例子中。我的项目使用 Facebook SDK,我想为我的 NSDictionary 创建包含好友列表的单例类。我的 .m 委托文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//here is some other code
facebook = [[Facebook alloc] initWithAppId:@"my app id" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_location",
@"friends_location",
@"read_friendlists",
nil];
[facebook authorize:permissions];
[permissions release];
}
[facebook requestWithGraphPath:@"me/friends" andDelegate:(id)self];
//here is some other code
}
我设置了返回好友列表的请求的 NSDictionary 值:
- (void)request:(FBRequest *)request didLoad:(id)result {
_friendsDictionary = result;
}
我需要以哪种方式编写单例类而不使用委托类AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
。