简体   繁体   中英

Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type

Getting this error on size = text.boundingRectWithSize

Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional>.Type) to expected argument type '[String : AnyObject]?'

func heightOfString(text: NSString!, width: CGFloat, font: UIFont) -> CGFloat{
    var ch:CGFloat!
    var size:CGSize = CGSizeMake(width, CGFloat.max)

    var tdic:NSDictionary = NSDictionary(objects: [font, NSFontAttributeName], forKeys: [])
    size = text.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesFontLeading , attributes: [String : AnyObject]?, context: nil).size
    ch = size.height
    return ch

}

如果您需要为属性传递一个空字典,请使用[:]

size = text.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesFontLeading , attributes: [:], context: nil).size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM