Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

unwrap

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?

like image 220
raed Avatar asked Dec 03 '25 18:12

raed


1 Answers

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.

like image 68
vadian Avatar answered Dec 06 '25 08:12

vadian