我有以下代码,但在错误转换时遇到错误,仍然在学习下面的代码到底要做什么,因此尚不确定代码的含义。

 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]?'

最佳答案

无需将attributes强制转换为NSDictionary。进行以下更改:

let attributes =  [NSFontAttributeName: font] as NSDictionary


应该

let attributes =  [NSFontAttributeName: font]


并删除投放到[NSObject : AnyObject]

09-10 06:23
查看更多