NSObject无法转换字符串

时间:2016-01-28 13:48:25

标签: ios swift

我有以下代码,但是我在转换错误时遇到错误,仍在了解下面的代码究竟是做什么的,所以不确定代码的含义。

 let padding = 353
 let minHeightText: NSString = "\n\n"
 let font = UIFont(name: "Avenir Light", size: 15.0)!
 let attributes =  [NSFontAttributeName: font] as NSDictionary
 let item = items[indexPath.row]
 let minSize = minHeightText.boundingRectWithSize(CGSize(width: (view.frame.size.width - 40), height: 1000), options: .UsesLineFragmentOrigin, attributes: attributes as [NSObject : AnyObject] as [NSObject : AnyObject], context: nil).height
 let maxSize = item.itemDesctiption.boundingRectWithSize(CGSize(width: (view.frame.size.width - 40), height: 1000), options: .UsesLineFragmentOrigin, attributes: attributes as [NSObject : AnyObject] as [NSObject : AnyObject], context: nil).height + 50

我得到的错误就是这个..

/Users/David/Desktop/iOS_app/Bid-Hub-app/iOS-app/BidHub-iOS-master/AuctionApp/ItemList/ItemListViewController.swift:140:215: Cannot convert value of type '[NSObject : AnyObject]' to expected argument type '[String : AnyObject]?'
/Users/David/Desktop/iOS_app/Bid-Hub-app/iOS-app/BidHub-iOS-master/AuctionApp/ItemList/ItemListViewController.swift:138:208: Cannot convert value of type '[NSObject : AnyObject]' to expected argument type '[String : AnyObject]?'

1 个答案:

答案 0 :(得分:0)

无需将attributes投射到NSDictionary。进行以下更改:

let attributes =  [NSFontAttributeName: font] as NSDictionary

应该是

let attributes =  [NSFontAttributeName: font]

并将转换移至[NSObject : AnyObject]