ios 从 AnyObject(NSString) 获取到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24005300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:16:05  来源:igfitidea点击:

Get from AnyObject(NSString) to String

iosnsstringswift

提问by Daij-Djan

I am reading a plist key (NSArray with n NSDictionaries):

我正在阅读 plist 键(带有 n 个 NSDictionaries 的 NSArray):

    let regionsToMonitor = NSBundle.mainBundle().infoDictionary["Regions"] as Array<Dictionary<String,AnyObject>>

now I iterate over it:

现在我迭代它:

    for regionToMonitor in regionsToMonitor {

and now I want to to get uuidString of the regionToMonitor

现在我想获取 regionToMonitor 的 uuidString

in ObjC: NSString *uuidString = regionToMonitor[@"uuidString"];

在 ObjC 中: NSString *uuidString = regionToMonitor[@"uuidString"];

in swift I try: let uuidString = regionToMonitor["uuid"]!.stringValue;

我迅速尝试: let uuidString = regionToMonitor["uuid"]!.stringValue;

the above does compile but the string is always nilin swift. regionToMonitor["uuid"]when used without !.stringValue works fine in println

上面确实可以编译,但字符串总是nil很快。regionToMonitor["uuid"]在没有 !.stringValue 的情况下使用时可以正常工作println

how do I get a valid Swift.String here?

我如何在这里获得有效的 Swift.String?

I am trying to pass it to NSUUID!

我正在尝试将其传递给 NSUUID!



I also tried

我也试过

let uuidString:String = regionToMonitor["uuid"]
=> AnyObject isn't convertible to String

let uuidString:String = regionToMonitor["uuid"]
=> AnyObject 不能转换为 String

let uuidString = regionToMonitor["uuid"] as String
=> Could not find an overload for 'subscript' that accepts the supplied arguments

let uuidString = regionToMonitor["uuid"] as String
=> 找不到接受所提供参数的“下标”的重载

let uuidString = regionToMonitor["uuid"];
=> 'AnyObject?' cannot be implicitly downcast to 'String'; did you mean to use 'as' to force downcast?

let uuidString = regionToMonitor["uuid"];
=> '任何对象?' 不能隐式向下转换为“字符串”;你的意思是用'as'来强制沮丧吗?

回答by Daij-Djan

I ended up with the ugly line:

我最终得到了丑陋的一行:

var uuidString:String = regionToMonitor["uuid"] as! String

no warnings, no errors, no runtime error

没有警告,没有错误,没有运行时错误

回答by Amitay

I found this to work for me

我发现这对我有用

var uuidString: String? = regionToMonitor["uuid"] as AnyObject? as? String

EDIT: this was the answer for an older swift version

编辑:这是较旧的 swift 版本的答案

Please use the accepted answer.

请使用已接受的答案。

回答by rickster

AnyObject?is an optional, because the dictionary may or may not contain a value for the "uuid" key. To get at an optional's value, you have to unwrapit. See Optionalsin the documentation.

AnyObject?是可选的,因为字典可能包含也可能不包含“uuid”键的值。要获得可选项的值,您必须解开它。见选配的文件中。

The safest way to deal with an optional is to put it in a conditional statement.

处理可选项最安全的方法是将它放在条件语句中。

if let uuidString = regionToMonitor["uuid"] {
    // do something with uuidString
}

If you're absolutely positively sure the dictionary will always contain this key/value pair, you can use an implicitly unwrapped optional (the !suffix):

如果您绝对肯定字典将始终包含此键/值对,则可以使用隐式展开的可选(!后缀):

println("UUID: \(regionToMonitor["uuid"]!)")       

In this case, if there's no value for the key your app will crash.

在这种情况下,如果键没有值,您的应用程序就会崩溃。

If you use !a lot, it looks like you're yelling all the time... which might help illustrate why you should use it sparingly, if at all. :)

如果你经常使用!它,看起来你一直在大喊大叫......这可能有助于说明为什么你应该谨慎使用它,如果有的话。:)

回答by Leandros

I've found a working solution, which compiles without warnings and such:

我找到了一个有效的解决方案,它可以在没有警告的情况下进行编译,例如:

var regions = NSBundle.mainBundle().infoDictionary["Regions"] as Array<Dictionary<String, AnyObject>>

for region in regions {
    let dict: NSDictionary = region
    var uuid = dict["uuidString"] as String
}

The infoDictionaryfrom the NSBundlereturns an NSArrayand NSDictionary, not a Swift.Arrayor Swift.Dictionary. Though, they should be interchangeable, but maybe they aren't as we though.

infoDictionaryNSBundle回报的NSArrayNSDictionary,而不是一个Swift.ArraySwift.Dictionary。虽然,它们应该可以互换,但也许它们不像我们那样。

回答by ACengiz

I am not sure my solution is effective of not but here it is.

我不确定我的解决方案是否有效,但在这里。

var uuidVar = regionToMonitor["uuid"]
var uuidString:String = "\(uuidVar)"

Hope it helps.

希望能帮助到你。

回答by ggomeze

If you are sure you want the unwrapped value you can use any of these:

如果你确定你想要解包的值,你可以使用以下任何一个:

var uuidString:String! = regionToMonitor["uuid"]
var uuidString = regionToMonitor["uuid"] as String!

or even this:

甚至这个:

if var uuidString = regionToMonitor["uuid"] {
    println("\(uuidString) has been unwrapped")
}

回答by Mike Seghers

You can also use

你也可以使用

var uuidString = regionToMonitor["uuid"]? as String

It has the same results as what you are doing, but is IMHO more clear in intent. The asoperator force unwraps anyway, so putting the exclamation mark behind it feels redundant. Putting the question mark behind the dictionary subscript makes it clear you are chaining an optional.

它的结果与您正在做的事情相同,但恕我直言,意图更明确。该as操作力解开无论如何,所以把感叹号后面感觉多余的。将问号放在字典下标后面可以清楚地表明您正在链接一个可选项。

回答by Scodino

Keep it simple:

把事情简单化:

let uuidString = "\(regionToMonitor["uuid"])"