下面的代码行出现了错误。因为这是我刚刚从github下载的一个项目,https://github.com/HubSpot/BidHub-iOS,我不知道这些行在做什么。
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)错误如下
/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]?'所以就在你建议我把多余的
as [NSObject : AnyObject]已经尝试过了,它会给出以下错误
/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?编辑:
var bidAttrs = [NSFontAttributeName : UIFont(name: "Avenir-Light", size: 14.0)! , NSForegroundColorAttributeName: UIColor.grayColor()] as NSDictionary发布于 2016-01-28 13:39:45
您不需要将bidAttrs转换为NSDictionary,所以只需这样做:
var bidAttrs = [NSFontAttributeName : UIFont(name: "Avenir-Light", size: 14.0)! , NSForegroundColorAttributeName: UIColor.grayColor()]
let one = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs )但是,如果出于某种原因需要它成为一个NSDictionary,那么您的转换应该如下所示:
let one = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs as? [String : AnyObject] )https://stackoverflow.com/questions/35061834
复制相似问题