在我UIFontExtension.swift
文件,我得到的是指明瞭斯威夫特編譯器錯誤...可選類型'NSNumber'的值未解包;你的意思是使用'!'要麼 '?'?
Value of optional type 'NSNumber' not unwrapped; did you mean to use '!' or '?'?
此錯誤指向w!
和h!
上,我都不得不結束註釋行。
import Foundation
import UIKit
extension UIFont {
func sizeOfString (string: String, maxWidth w: CGFloat?, maxHeight h: CGFloat?) -> CGSize {
var width = w != nil ? w! : DBL_MAX // Error on this line
var height = h != nil ? h! : DBL_MAX // And on this line
var size = CGSize(width: width, height: height)
var options = NSStringDrawingOptions.UsesLineFragmentOrigin
var attributes = [NSFontAttributeName: self]
var nsstring = NSString(string: string)
return nsstring.boundingRectWithSize(size, options: options, attributes: attributes, context: nil).size
}
}
這只是一個錯誤?
我斯威夫特REPL不會產生你所描述的錯誤。 – GoZoner
有趣。如果不使用本地別名(在函數中使用maxWidth和maxHeight而不是w和h),它會發生嗎? –