Why is it so expensive in Swift compared to just ... leaving it as AnyObject?

I am getting the value from NSUserDefaults

, the value I am sure is an array of String

s. There are tens of thousands of lines in the array. I am doing the following:

let identifiers = NSUserDefaults.standardUserDefaults().objectForKey("UserIdentifiers") as! [String]

      

Take 0.5 seconds for processing on iPhone 6.

Delete the cast at the end? 0.000107 seconds .

Does it check every element of the array to make sure it's a string? Can't it do it?

Even if I just throw it as [AnyObject], it still takes about 0.3 seconds.

+3


source to share


1 answer


Lucas Kukacka is right ... Fading starts when you start using NSUserDefaults to store a huge amount of values. You are using NSUserDefaults for small amounts of data that you need to retrieve globally. You should rather make a service class to do this, rather than the built-in UserDefaults.

As far as type casting is concerned, String is fast and NSString is much faster than String. U may lose some functionality, but it will be much faster. Check out the following link for a preface:



http://en.swifter.tips/string-nsstring/

0


source







All Articles