首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSObject无法转换

NSObject无法转换
EN

Stack Overflow用户
提问于 2016-01-28 12:42:41
回答 1查看 171关注 0票数 0

下面的代码行出现了错误。因为这是我刚刚从github下载的一个项目,https://github.com/HubSpot/BidHub-iOS,我不知道这些行在做什么。

代码语言:javascript
复制
  let one = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs as [NSObject : AnyObject] as [NSObject : AnyObject] )
    one.appendAttributedString(NSMutableAttributedString(string: "$\(startAmount + incrementOne)", attributes: otherAttrs))
    plusOneButton.setAttributedTitle(one, forState: .Normal)

    let five = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs as [NSObject : AnyObject] as [NSObject : AnyObject])
    five.appendAttributedString(NSMutableAttributedString(string: "$\(startAmount + incrementFive)", attributes: otherAttrs))
    plusFiveButton.setAttributedTitle(five, forState: .Normal)

    let ten = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs as [NSObject : AnyObject] as [NSObject : AnyObject])
    ten.appendAttributedString(NSMutableAttributedString(string: "$\(startAmount + incrementTen)", attributes: otherAttrs))
    plusTenButton.setAttributedTitle(ten, forState: .Normal)

错误如下

代码语言:javascript
复制
/Users/David/Desktop/iOS_app/Bid-Hub-app/iOS-app/BidHub-iOS-master/AuctionApp/BiddingVC/BiddingViewController.swift:104:109: 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/BiddingVC/BiddingViewController.swift:112:109: 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/BiddingVC/BiddingViewController.swift:108:110: Cannot convert value of type '[NSObject : AnyObject]' to expected argument type '[String : AnyObject]?'

所以就在你建议我把多余的

代码语言:javascript
复制
as [NSObject : AnyObject]

已经尝试过了,它会给出以下错误

代码语言:javascript
复制
/Users/David/Desktop/iOS_app/Bid-Hub-app/iOS-app/BidHub-iOS-master/AuctionApp/BiddingVC/BiddingViewController.swift:104:74: 'NSDictionary' is not implicitly convertible to '[NSObject : AnyObject]'; did you mean to use 'as' to explicitly convert?

编辑:

代码语言:javascript
复制
var bidAttrs = [NSFontAttributeName : UIFont(name: "Avenir-Light", size: 14.0)! , NSForegroundColorAttributeName: UIColor.grayColor()] as NSDictionary
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-28 13:39:45

您不需要将bidAttrs转换为NSDictionary,所以只需这样做:

代码语言:javascript
复制
var bidAttrs = [NSFontAttributeName : UIFont(name: "Avenir-Light", size: 14.0)! , NSForegroundColorAttributeName: UIColor.grayColor()]
let one = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs )

但是,如果出于某种原因需要它成为一个NSDictionary,那么您的转换应该如下所示:

代码语言:javascript
复制
let one = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs as? [String : AnyObject] )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35061834

复制
相关文章

相似问题

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