JackyBoi
JackyBoi

Reputation: 2773

NSObject unable to convert

I am getting errors on the following lines of code. Since this is a project I just downloaded from github,https://github.com/HubSpot/BidHub-iOS I am not sure what these lines are doing.

  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)

Error is as follows

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

So just before you suggest me to remove the extra

as [NSObject : AnyObject]

Already tried that, and it gives the following error

/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?

EDIT:

var bidAttrs = [NSFontAttributeName : UIFont(name: "Avenir-Light", size: 14.0)! , NSForegroundColorAttributeName: UIColor.grayColor()] as NSDictionary

Upvotes: 0

Views: 172

Answers (1)

konrad.bajtyngier
konrad.bajtyngier

Reputation: 1786

You don't need to cast bidAttrs to NSDictionary, so simply do this:

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

However, if you need it to be an NSDictionary for some reason, your casting should look like this:

let one = NSMutableAttributedString(string: "BID\n", attributes: bidAttrs as? [String : AnyObject] )

Upvotes: 2

Related Questions