首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能用“String”类型的索引订阅类型为“[NSObject:AnyObject]”的值

不能用“String”类型的索引订阅类型为“[NSObject:AnyObject]”的值
EN

Stack Overflow用户
提问于 2018-05-02 18:23:57
回答 3查看 133关注 0票数 2

我在下面的代码片段中发现了错误。每次我试图构建它时,编译器都会抱怨:

不能用“String”类型的索引下标“NSObject:AnyObject”类型的值

这是代码,它的所有荣耀:

代码语言:javascript
复制
import Foundation
import MapKit

enum LocationKey: String {
    case Latitude = "lat"
    case Longitude = "long"
    case Title = "title"
}

extension MKPointAnnotation {
    var propertyState: [NSObject: AnyObject] {
        get {
            return [ LocationKey.Longitude.rawValue as NSObject: NSNumber(value: coordinate.latitude),
                     LocationKey.Longitude.rawValue as NSObject: NSNumber(value: coordinate.longitude),
                     LocationKey.Title.rawValue as NSObject: title as AnyObject]
        }
        set {
            let lat = (newValue[LocationKey.Latitude.rawValue] as NSNumber).doubleValue
            let long = (newValue[LocationKey.Longitude.rawValue] as NSNumber).doubleValue
            coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
            title = newValue[LocationKey.Title.rawValue] as NSString
        }
    }
}

我真正遇到麻烦的代码行是:

代码语言:javascript
复制
let lat = (newValue[LocationKey.Latitude.rawValue] as NSNumber).doubleValue
let long = (newValue[LocationKey.Longitude.rawValue] as NSNumber).doubleValue
title = newValue[LocationKey.Title.rawValue] as NSString

谢谢一堆人!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-02 18:37:21

你的代码太复杂了。所有值都是值类型,因此请将字典声明为[String:Any] --顺便说一句,这解决了错误--并消除了对NSNumberAnyObject的所有丑陋类型转换。

代码语言:javascript
复制
import Foundation
import MapKit

enum LocationKey: String {
    case latitude = "lat"
    case longitude = "long"
    case title = "title"
}


extension MKPointAnnotation {
    var propertyState: [String: Any] {
        get {
            return [ LocationKey.latitude.rawValue: coordinate.latitude,
                     LocationKey.longitude.rawValue: coordinate.longitude,
                     LocationKey.title.rawValue: title ?? ""]
        }
        set {
            guard let lat = newValue[LocationKey.latitude.rawValue] as? CLLocationDegrees,
            let long = newValue[LocationKey.longitude.rawValue] as? CLLocationDegrees else { return }
            coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
            title = newValue[LocationKey.title.rawValue] as? String
        }
    }
}

您甚至可以使用枚举作为密钥。

代码语言:javascript
复制
extension MKPointAnnotation {
    var propertyState: [LocationKey: Any] {
        get {
            return [ .latitude: coordinate.latitude,
                     .longitude: coordinate.longitude,
                     .title: title ?? ""]
        }
        set {
            guard let lat = newValue[.latitude] as? CLLocationDegrees,
            let long = newValue[.longitude] as? CLLocationDegrees else { return }
            coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
            title = newValue[.title] as? String
        }
    }
}

注意:您的getter使用了两次LocationKey.Longitude,这将导致编译器错误。

票数 1
EN

Stack Overflow用户

发布于 2018-05-02 18:27:09

var propertyState: [NSObject: AnyObject]是一个字典,它有NSObject类型的键。尝试将其更改为键入String,并查看它是否有效。

票数 1
EN

Stack Overflow用户

发布于 2018-05-02 18:29:46

这是因为您正在创建一个带有NSObject键的字典。

试试这个:

代码语言:javascript
复制
import Foundation
import MapKit

enum LocationKey: String {
    case Latitude = "lat"
    case Longitude = "long"
    case Title = "title"
}

extension MKPointAnnotation {
    var propertyState: [String: AnyObject] {
        get {
            return [ LocationKey.Longitude.rawValue: NSNumber(value: coordinate.latitude),
                     LocationKey.Longitude.rawValue: NSNumber(value: coordinate.longitude),
                     LocationKey.Title.rawValue: title as AnyObject]
        }
        set {
            guard let lat = (newValue[LocationKey.Latitude.rawValue] as? NSNumber)?.doubleValue,
                let long = (newValue[LocationKey.Longitude.rawValue] as? NSNumber)?.doubleValue else {
                return
            }
            coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
            title = newValue[LocationKey.Title.rawValue] as? String
        }
    }
}

在代码中,您已经在properyState setter中看到了properyState选项,因为不使用强制展开是一种很好的做法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50141190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档