Swift - what's big-o of String(text[..<index]) - ios

When I learned python big-O of string[:index] was O(index).
However I've read apple developer document and swift's String is bit different from other languages. And there's no any documents about big-O of making substring from existing String.
I got curious about big-O of String(text[..<index]) in Swift so is there anyone who can tell me the big-O of String(text[..<index])

String is a collection of characters under the hood and String conforms to BidirectionalCollection, which protocol declares the subscript(bounds:) function as a required method. This change was introduced as part of SE-0163 and implemented in Swift 4.
Looking at the documentation of String.subscript(bounds:), it states that this method has O(1) complexity.

Related

Are there considerable drawbacks to use Swift's ContiguousArray? If so, which are there?

Apple's ContiguousArray Documentation describes it as:
The ContiguousArray type is a specialized array that always stores its elements in a contiguous region of memory. This contrasts with Array, which can store its elements in either a contiguous region of memory or an NSArray instance if its Element type is a class or #objc protocol.
If your array’s Element type is a class or #objc protocol and you do not need to bridge the array to NSArray or pass the array to Objective-C APIs, using ContiguousArray may be more efficient and have more predictable performance than Array. If the array’s Element type is a struct or enumeration, Array and ContiguousArray should have similar efficiency.
To me this sounds like a ContiguousArray is always preferable to a regular Array since it has at least the same performance. Yet in my (perhaps too limited) experience with swift so far I've never seen anybody use a ContiguousArray instead of an Array and the description seems almost too good to be true.
I could not find any lists of drawbacks of ContiguousArrays online. I could imagine that while it is running faster, it has a less favourable memory usage than an array, but I can't find any information backing this. Can somebody with more understanding of the matter please help me to understand if there's a trade-off to expect when using ContiguousArrays?

What does <~~ mean in swift?

While checking a parser of a JSON in swift, I found the following code:
description = "desc" <~~ json
I suppose that it is similar to use the following:
description = json["desc"]
Is it correct? if no, what does this operator mean?
Thanks
You are right. But it would be wrong to assume that's what it is set out to do in Swift.
I think the parser that was being used was Gloss, and it seems that they have written an operator overload specifically to mean description = json["desc"] (and or or some other stuff under the hood to make the parsing easier) . The operator does not have a meaning per se in Swift. But it's invented by the framework to do the parsing.
You can read about operator overloading here
EDIT
I always have incorrectly used the terms operator overloading and defining custom operator interchangeably. Operator Overloading is extending the implementation of the existing operators which is different than defining your own custom operators. Thank you SO MUCH for pointing this out, #Giacomo Alzetta!

Java null vs Swift nil

I'm coming from a Java/Android background where we use NULL. Now I am doing Swift/iOS and I am confused as to what Swift's nil means.
Can I use it like NULL in Java? Does it act the exact same way or is there something different about its usage?
You can think of 'null' and 'nil' the same. Whether the language includes optionals is a separate concern.
Objective-C, has 'nil', but does not have in-built optionals while Swift does. Similarly, Java has 'null', but not have implicit optionals, while several JVM languages such as Kotlin, Scala and Ceylon do, and did so before Swift. &nbsp
Here's an article that compares about null, nil and optionals in Kotlin, Scala and Swift: http://codemonkeyism.com/comparing-optionals-and-null-in-swift-scala-ceylon-and-kotlin/
Incidentally, for Android development you may want to investigate Kotlin and the associated Anko library from Jetbrains.
Swift does not currently support a null coalescing operator.
Swift nil explanation
nil means "no value". Non-optional variables cannot be assigned nil even if they're classes, so it's explicitly not a null pointer and not similar to one.
I can recommend you to read more about optionals in Swift.
Disclosure: I don't know a whole terrible lot about Java, so my answer is coming from a C++/Objective-C/Swift perspective.
Matt Thompson of NSHipster has a great post about it and how it relates to Objective-C vs C. You can find it here.
The answer basically boils down to this: you should consider it the same.
0 is how you represent nothing for a primitive value
NULL is how you represent nothing for literal for C pointers
nil is how you represent nothing for literal Objective-C/Swift objects

Decimals in Swift

I am writing a Swift app and am dealing with decimals in a database (stored in mysql as decimals, with 2 digits. Basically it's sales someone made each day, so generally anything from $0 to $1000, but not millions, and nothing insane in terms of trailing decimals, just always rounded to 2 decimal places).
Referencing this helped me out:
How to properly format currency on ios
..But I wanted to just do a quick sanity check here and make sure this strategy is ok.
i.e I would use NSDecimal or NSDecimalNumber (is there a preferred swift equivalent??)
What would you all recommend I do when dealing with currency in Swift? I'd like to use the locale-based currency symbol as well. I have a class called Sales that contains the amount in question. What do you recommend the datatype to be?
Apologies if I am coming off lazy, I actually have some ideas on what to do but feel a little overwhelmed at the "right" approach, especially in a locale-sensitive way, and wanted to check in here with you all.
Thanks so much!
Update for Swift 3: A Decimal type is now available with built-in support for operators like *, /, +, <, etc. When used in an Any context (passed to Objective-C), it's bridged to NSDecimalNumber.
Old answer:
NSDecimal is not really supported in Swift (it's a weird opaque pointer type), but NSDecimalNumber is — and as in Obj-C, it's the best thing to use for base-ten arithmetic (because it actually does its operations in base ten). NSLocale, NSNumberFormatter and friends all work too and should satisfy your localization needs.
Swift 3 now has a Decimal (value) type which is bridged to NSDecimalNumber.

Why one would use array in functional programming (erlang) instead of lists?

when you are going to use array module - and when to use arrays generally in functional programming - erlang in this case.
Thanks.
The rationale is that if you do want a functional (nondestructive) data structure using integer keys, then the array module is significantly more efficient than a dict, gb_tree or similar (which can use any kinds of values as keys). And the indexing is zero-based because that's generally more useful for the kind of problems you'd want an array for.
I find the note in Learn You Some Erlang to be quite off the mark.

Resources