我有一个名为LinkedInLoginController
的Objective-C控制器,在其中我有一个函数,其中包含一个名为newResult的NSDictionary,如下所示:
- (void)requestMeWithToken:(NSString *)accessToken {
[self.client GET:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result)
{
NSLog(@"current user %@", result);
NSMutableDictionary *newResult = [result mutableCopy]; //copies the NSDictionary 'result' to a NSMutableDictionary called 'newResult'
[newResult setObject: [newResult objectForKey: @"id"] forKey: @"AppUserID"]; //copies the value assigned to key 'id', to 'AppUserID' so the database can recognize it
[newResult removeObjectForKey: @"id"]; //removes 'id' key and value
LinkedInLoginJSON *instance= [LinkedInLoginJSON new]; //calls LinkedInPost method from LinkedInLoginJSON controller
[instance LinkedInPost:newResult];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"failed to fetch current user %@", error);
}
];
[self performSegueWithIdentifier:@"toHotTopics" sender: self]; //Sends user on to Trending page after they sign in with LinkedIn
}
我正在尝试将AppUserID
的键值传递给四个不同的Swift ViewControllers。做这个的最好方式是什么?用segueway将它传递过来?我尝试使用本指南直接从NSDictionary中调用它,但没有成功:http://mobiforge.com/design-development/using-objective-c-and-swift-together-ios-apps
答案 0 :(得分:1)
创建nsobject的单例,新文件 - > cocoaclass子类 在.h文件中
#import <Foundation/Foundation.h>
@interface ShowTimer : NSObject
{
NSMutableDictionary *passed;
}
@property (nonatomic, retain) NSMutableDictionary *passed;
+ (id)sharedManager;
@end
.m文件
#import "ShowTimer.h"
@implementation ShowTimer
@synthesize passed;
#pragma mark Singleton Methods
+ (id)sharedManager {
static ShowTimer *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
}
@end
在视图控制器中导入#import "ShowTimer.h"
然后设置或获取对象。
设置你的对象
ShowTimer *timer=[ShowTimer sharedManager];
timer.passed=newResult;
获取您的对象
ShowTimer *timer=[ShowTimer sharedManager];
NSMutableDictionary *newResult =timer.passed;
对于命名很抱歉,我只是复制并粘贴了另一个类来回答您的问题,但如果您愿意,可以更改命名....
编辑:
当你向swift添加一个objective-c类时,你会选择桥接选项,如果你说是,它会在你的项目中添加-Bridging-Header.h
,只需调用#import "ShowTimer.h"
你的swift视图控制器中的
使用像这样的代码
var companies:NSMutableDictionary = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"]
var instance:ShowTimer = ShowTimer.sharedManager() as ShowTimer
instance.passed=companies
var dict:NSMutableDictionary=instance.passed
NSLog("%@", dict);
我之前从未编写过快速代码,但如果我的方法对你不起作用,研究就会点亮
答案 1 :(得分:1)
幸运的是,Obj-C和Swift目前是合作兼容的(根据我的理解)。
在Obj-C中我会创建一个新的NSObject子类,也许是MySharedValues,所以标题看起来像这样:
@interface MySharedValues : NSObject
@property (nonatomic, strong) id appUserId;
+(instancetype)defaultInstance;
@end
实施方式如下:
@implementation
+(instancetype)defaultInstance{
static MySharedValues *obj = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
obj = [[self alloc] init];
});
return obj;
}
@end
然后,您可以将#import "MySharedValues.h"
导入所有控制器的标题中。要访问或设置值,您可以调用
[MySharedValues defaultInstance].appUserId;