What are lists in Swift, and how can I use subscripts with a list? [closed] - swift

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
The Swift Programming Guide (Swift 5.1 Edition) says that subscripts can be used to access members of “a collection, list, or sequence.” Collection and Sequence are defined protocols in Swift and are well documented. Do lists exist in Swift as a separate entity? If so, what is the syntax for a list subscript?

It would be better if The Swift Programming Language (the book) didn't say “ a collection, list, or sequence”. As you point out, Swift has standard Collection and Sequence types. It does not have any standard type named List.
The closest thing would be Array:
let words: Array<String> = ["Mark", "Cowan"]
// or words: [String]
let firstWord = words[0]

Related

What is the difference between function and function(_:) in Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've recently used flatMap to map String? to Double? in Swift. Calling:
optionalString.flatMap(Double.init)
and
optionalString.flatMap(Double.init(_:))
produces the same result. I have two questions:
Is there a difference between Double.init and Double.init(_:)?
I did not think that you can use function signatures in real Swift code. What are some other examples where you can use these?
No, there is no difference between Double.init and Double.init(_:).
These are called first class functions in Swift.
For example:
extension UIView {
func addSubviews(_ views: UIView...) {
views.forEach(addSubview)
}
}
More info: First class functions in Swift
Edit: There is a difference between Double.init and Double.init(_:). ( check comments )

How can i derive values list of values from list of either using flatmap for fold? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
im trying to convert a scala list of eithers, as such:
List(Left(3),Left(4),Left(1),Left(5))
Into a either of a list Either[List[Int],Int] like this?
Left(3,4,1,5)
Only using flatmap, map or fold?
ive been hammering at it for a while now and can simply not make it work
Assumed:
val a = List(Left(1), Left(2), Left(3)) // for example
Then following will return a Left[List[Int]]:
Left(a.map(_.value))
// Left(List(1, 2, 3))
Then you can extract the values out of the list, which I don't thin is generally a good idea.

List of Swift Data Types [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list of Swift Data Types, and I want to know if I missed any.
Here is my list:
Int
Double
Bool
String
Character
Optional
Any help appreciated.
This is absolutely, completely wrong. The five categories of types are:
Class
Struct
Enum
Tuple
Closure
Int, Double, Bool, String and Character are structs, and Optional is an Enum. But if these are the ones you count as type, then each of the five type categories can contain an infinite number of types.
You got a link to a tutorial site that supposedly listed all built-in types. None of these types are built into the language. Many are built into the Swift library. There is syntactic sugar for "Optional" built into the language, but in reality the various "Optional" types are just Enums with two cases "none" and "some".

scala get vs match which one to use, given that restult will always be same type? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Given that match will never be None,
Which style is better or can I improve the first?
val tmp = (cols.find(_(0) == id).get)
SomeClass(tmp(0), tmp(1))
cols.find(_(0) == id) match {
case Some(value)=> SomeClass(value(0), value(1))
case None=> NotFound("Given id not found")
}
Since this a question about style, my answer is that neither of these is the best style. Instead, just keep the value in the Option
val opt: Option[SomeClass] = cols.find(_(0) == id).map(v => SomeClass(v(0), v(1)))
Keep processing/testing inside the Option using foreach/exists etc. until you really need the bare value. There is a very rich set of methods on Option that covers most of the things that are needed.
If the rest of the code is structured well, you will likely find that the value never needs to be extracted in a separate operation.

Swift 3 names (uppercase or not)? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am new in Swift.
Where can I find an overview for the right names in Swift 3?
Classnames = classNameOne
String: myString
Function: myFunction(myTest : string, myNumber : Int)
Struct: myStructOne
Label: lblOne
Button: btnTwo or cmd?
Image: imgThree
I hope you understand what I mean.
Of course there is no rule for this (But there are Guidelines). But the common accepted way is like this (I put in bold the ones that change from what you wrote):
Classnames = ClassNameOne
String: myString
Function: myFunction(myTest : string, myNumber : Int)
Struct: MyStructOne
Label: lblOne
Button: btnTwo
Image: imgThree
For the most part naming conventions are relatively subject to opinion, but the syntax of your naming conventions should be universal. Ray Wenderlich has a great style guide for Swift that I believe all Swift developers should follow. It adhears to the "Camel-Case" of first letter in the class name is capitalize as well as each successive word (class MyClass) and keeping the first character of each variable name as lowercase, then each successive word is capitalized (var myVariable).
As with any language, there's no one way everyone does things.
The only "official" source is Swift's official guidelines. Beyond that it depends on you and/or your company's preferences.