Ambiguous reference to member 'append' [closed] - swift

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am beginner in Swift and learning swift from "The Swift Programming language(Swift 3 beta)". In this book, there is one example of creating generic function but I am getting the above mentioned error.
func makeArray<Item>(repeating item: Item, numberOfTimes: Int)->[Item]
{
var result = [Item]()
for _ in 0..<numberOfTimes {
return.append(item)//Error here.
}
return result
}

This line:
return.append(item)
should be:
result.append(item)
You cannot append to return, it only returns the value.

Related

Print only unique element from a list in scala [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 1 year ago.
Improve this question
Consider the following list(1,1,2,2,3,4,4,5,5) I want to print only 3. Since it is the unique one. Can someone help me with the Scala code.
It's not a Spark question, but this function would do it for Scala
def uniqueElems[T](lst: Seq[T]) = {
lst.groupBy(identity).collect { case v if v._2.length == 1 => v._2.head }
}

Why I got error on Array("a", "b", "c").map(x => x.toInt) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Tried to get integers from a list of Array with letters. While I can use "a".toInt to get 97, why cant I use the map function on an Array to get Array(97,98,99)?
Are you sure you can use "a".toInt? Because here a is a string and cannot be converted.
'a'.toInt on the other hand, works perfectly

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 )

What are lists in Swift, and how can I use subscripts with a list? [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 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]

Using a function's argument to get the value of the substructure [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
function found = find(property)
res = 0;
if subject(1).property == 20
res = res + 1;
end
end
Hey guys, I am trying to get the substructure to be identified through this function's argument. Any ideas on how to implement this? Thank you.
if subject(1).(property) == 20
I finally figured it out, I just had to put parentheses.