问题描述
我正在读一个plist键(带有n个NSDictionaries的NSArray):
I am reading a plist key (NSArray with n NSDictionaries):
let regionsToMonitor = NSBundle.mainBundle().infoDictionary["Regions"] as Array<Dictionary<String,AnyObject>>
现在我迭代它:
for regionToMonitor in regionsToMonitor {
现在我想获得ObjC中regionToMonitor的uuidString
and now I want to to get uuidString of the regionToMonitor
: NSString * uuidString = regionToMonitor [@uuidString];
在swift中我尝试:让uuidString = regionToMonitor [uuid]!。stringValue;
in swift I try: let uuidString = regionToMonitor["uuid"]!.stringValue;
以上编译但是字符串在swift中总是 nil
。 regionToMonitor [uuid]
在没有!.stringValue的情况下使用 println
the above does compile but the string is always nil
in swift. regionToMonitor["uuid"]
when used without !.stringValue works fine in println
如何在这里获得有效的Swift.String?
how do I get a valid Swift.String here?
我试图将其传递给NSUUID!
我也试过
让uuidString:String = regionToMonitor [uuid]
=> AnyObject不能转换为String
let uuidString:String = regionToMonitor["uuid"]
=> AnyObject isn't convertible to String
让uuidString = regionToMonitor [uuid] as String
=>找不到接受提供参数的'subscript'的重载
let uuidString = regionToMonitor["uuid"] as String
=> Could not find an overload for 'subscript' that accepts the supplied arguments
让uuidString = regionToMonitor [uuid];
=>'AnyObject?'不能被隐含地转向'String';你的意思是使用'as'强制向下倾斜?
let uuidString = regionToMonitor["uuid"];
=> 'AnyObject?' cannot be implicitly downcast to 'String'; did you mean to use 'as' to force downcast?
推荐答案
我最终得到了丑陋的一句话:
I ended up with the ugly line:
var uuidString:String = regionToMonitor["uuid"] as! String
没有警告,没有错误,没有运行时错误
no warnings, no errors, no runtime error
这篇关于从AnyObject(NSString)获取String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!