[英]access objective-c variable in swift
大家好,我的故事板中有两个视图控制器,一个 tableViewController 和一个 Normal ViewController。 tableViewController 中的按钮(不在单元格中)转到第二个视图。 但是 tableViewController 有一个我不是自己编写的objective-c 类。
// When data is comming, this will be called
- (void)bleDidReceiveData:(unsigned char *)data length:(int)length {
NSData *d = [NSData dataWithBytes:data length:length];
NSString *s = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
NSLog(@"received data: %@", s);
}
我想从快速的第二个视图控制器访问“s”。
例子:
var data : NSString = s
OC 方法的编写方式, s
只存在于该方法内部。 如果你想从不同类的代码中获取它,它需要是可公开访问的。
最简单的方法是从使用局部变量更改为使用属性来存储字符串。
根据我的理解,这是如何做到的。
在 Objective-C 类 .h(头文件)上
@class yourObjectiveCClass
@interface yourObjectiveCClass {
NSString *s;
}
@property (nonatomic, nullable) NSString *s;
@end
然后,在 Objective-C 类 .m 上
@interface yourObjectiveCClass ()
...
@end
@implementation yourObjectiveCClass
@synthesize *s;
@end
现在您可以从 Swift 文件访问 string 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.