Search code examples
iosswiftunwrap

Is there way to use NSParagraphStyle.default.mutableCopy() without force unwraping?


I m trying to get an instance of NSParagraphStyle.default.mutableCopy() but are we sure that mutableCopy() will always contain a value?

var paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle

Would it possible to do this without force unwraping?


Solution

  • Yes, it's much simpler:

    let paragraphStyle = NSMutableParagraphStyle() // Note the `let`
    

    You get the default parameters with the default initializer.


    Apart from that in this case you can be sure that mutableCopy() will always contain a value because NSParagraphStyle clearly conforms to NSCopying.