Iterating over a for loop X amount of times from textfield input - ios

I am trying to query my textfield.text input (Let's say the user types 5 in the keypad).
Then convert the string to an integer.
Then make a for loop and run the for loop X amount of times. (X being the integer converted from the string).
I have tried the below however I can't work out the syntax/format for the for loop.
var shotCountInt = Int(numberOfShots.text!)
for item in 0..<shotCountInt {
//do something
}
The error I am getting is on the for loop which is:
Type 'Int?' does not conform to protocol 'Sequence'

I think the problem is in 0..<shotCountInt of optional Int , so
if let shotCountInt = Int(numberOfShots.text!) {
for item in 0..<shotCountInt {
// proceed here
}
}

Related

Swift: Convert Int to UInt32, and how to unwrap optional

im pretty new to swift, and im just trying to build something to test out the waters. this is in relation to a previous question i had. I am building some code to take user input from a UITextField object, and basically im trying to figure out how to convert an Int to a UInt32, but nothing ive searched on SO or otherwise has really helped.
here is my code
//this is where i call the user input.
var rangeInput: Int? {
get {
return Int(rangeInputTextField?.text ?? "")
}
//this is my function to create a range, and call a random number out of that range
let viewController = ViewController()
var x = ViewController().rangeInput
let y = (Int?(x!))
var number = arc4random_uniform(Int(y!))//ERROR OCCURS HERE "Cannot convert value of type 'Int' to expected argument type 'UInt32'
//MARK: Class for random number
struct RandomNumber {
// numberRange to change the value for 1...X(user input)
//creates the list to be picked from. (pickRandom)
func numberRange(high: UInt32) ->Range<UInt32>{
if let high = UInt32?(0){
print("Invalid number")
} else { let high = Int(y!) + 1
}
let range = 1...high
return range
}
//pick random number from that list
let pickRandom = number
}
edit: Converted UInt, using answer in comments, but am having an issue with unwrapping optionals.
am I doing something wrong with forcibly unwrapping optionals?
let viewController = ViewController()
var x = ViewController().rangeInput
var number = arc4random_uniform(UInt32(x!)) // <----- UInt32 conversion
However, do recommend to check the user input x and/or after the UInt32 conversion, in case user inputs something not sensible, using guard or if let
Initializers are called in swift by listing the data type (UInt32 in this case) followed by parenthesis containing the parameters. For most basic data types in Swift, there are no parameters for the initializers. So if you ever want to make an Int, Float, Double, UInt32, String etc., just do
UInt32(Value)!//UInt32 can be substituted for any of the above values
The "!" unwraps the optional, and will result in an error in runtime like "unexpectedly found nil when unwrapping optional value" if the value is not a valid form of the data type, if you want to do this safely, you can do this
if UInt32(Value) != nil {
//Valid Value
}else {
//Invalid Value
}
Also this is unrelated to the question, but your if let statement will always be true because you are overriding the value of the parameter "high" to a UInt32 of 0. I could be wrong though.

"Cannot subscript a value of type '[String]' with an index of type 'String'

I'm trying to display a dynamically updated array in a label using:
for i in result {
outputLbl.text = result[i].joinWithSeparator("\n")
}
However I get the error
Cannot subscript a value of type '[String]' with an index of type 'String'.
Any idea how I can fix this?
Note that when using the loop "header" for X in Y, you don't get the indices of Y, but the actual elements of Y. Judging from your error, results is an array of strings ([String]). Hence, i in you for loop represents---one by one---the elements in the String array results.
So, if you wanted to access the string elements one by one in the for loop above, you could use the approach in your example:
let result = ["Hello", "World"]
for myString in result {
// use each string element in some manner...
}
However, as you are using the array method joinWithSeparator(..), you should use, just as Leo writes in his comment above, this method directly on your array (and not their elements!)
let result = ["Hello", "World"]
outputLbl.text = result.joinWithSeparator("\n")
/* Hello\nWorld */
I think what you are doing here is trying to iterate through an array of string and then update a label that is in this case "outputLbl".Here you can do something like this
for i in result {
//result is array of strings.
// here i is individual element of result array
/* outputLbl.text = result[i].joinWithSeparator(“\n”)*/
//instead you can write
outputLbl.text = i.joinWithSeparator(“\n”)
}
The reason you are getting this error is as follows:
It seems you are confusing the type of i. The variable result is of type [String] which means it is an array of String types. By virtue of being an array, it must be subscripted with an Int, not a String. So something like result[0] or result[12] is valid, but something like result["hello"] is not valid. The variable i here is a String because it is a single element in an array of String types, which means that effectively, what you're trying to do by saying result[i] is something along the lines of result["hello"].
That having been said, the true solution to your problem is that the method joinWithSeparator(_:String) is not a String method but rather a Sequence type method. Which means it should be called on a Sequence like an object of type [String]. So what you should use is:
outputLbl.text = result.joinWithSeparator("\n")
What's going on here is the compiler is inferring i to be of type String since that's what result is. You should be more verbose in your naming conventions.
for specificString in result {
outputLbl1.text += "\n\(specifcString)"
}
EDITED for correctness.

Random number generator function that doesn't repeat itself

Does swift by default has a random number generator that returns the same number only once?
For example it picks a number in range 1,2,3,4,5. Return that random number (lets say 3). In the next loop, it picks only from 1,2,4,5 and so on.
EDIT:
This is what I ended up using. It returns an array of integers from 0-5 and was tested in playground. Note that this is meant to be used when you are picking from a large set of Integers and not just 6.
func generateRandomArray() -> [Int]{
var randomImages: [Int] = [Int]()
var newRandomNumber = Int(arc4random_uniform(UInt32(6)))
while (randomImages.count < 6) {
if (randomImages.contains(newRandomNumber) == false){
randomImages.append(newRandomNumber)
}
newRandomNumber = Int(arc4random_uniform(UInt32(6)))
}
return randomImages
}
This kind of generator is not called a "random number" generator, but usually
a "Shuffle" or "Permute".
You have to tell it how many items there are first. Otherwise what you are proposing doesn't make any sense!
see answer here: How do I shuffle an array in Swift?
You can do this trick, Add the range of numbers from which you want to get the random result..to an NSMutableArray or NSMutableSet(to make sure there is only 1 of it).
Iterate through the array-
for(int i=0;i<myMutableArray .count;i++){
int randomIndex = arc4random_uniform(myMutableArray.count);
int myNumber = [[myMutableArray objectAtIndex:randomIndex]intValue]; // edited
NSLog(#"My random number-%i",myNumber);//this is your generated random number
[myMutableArray removeObjectAtIndex:randomIndex];
}
I guess this would do the tric, But if you do rmember NSMutableArray cannot take Primitive data type, just Objects like NSNumber.
EDIT
this line is not written and is justifying that why i am converting the NSNumber back to an interger because while adding the integer in the Array it has to be converted to a NSNumber|
[ myMutableArray addObject:[NSNumber numberWithInt:2]];
So in the for loop when i am getting the object from myMutableArray i am getting an NSNumber and to simplify it i casted that Object (NSNumber object) back to an integer.
From Apple's documentation:
A set stores distinct values of the same type in a collection with no
defined ordering.
This seems to be exactly what you are after, so making an array seems a bit ill-advised...
var values : Set<Int> = []
var value : Int {
return Int(arc4random() % 6 + 1)
}
while values.count < 6 {
values.insert(value)
}
And you probably want to have a look at those values somewhere down the line:
for value in values {
println(value)
}
Just store the last random number in a variable and check the next random number against it in a while loop. While the new random number is the same as the last random number, generate a new random number. Once the new random number generated is not the same as the last random number the while loop ends and we update the last random number.
var lastRandomNumber = 0
var newRandomNumber = 0
func gernerateRandomNumber() {
newRandomNumber = Int(arc4random_uniform(5)+1)
while newRandomNumber == lastRandomNumber {
newRandomNumber = Int(arc4random_uniform(5)+1)
}
lastRandomNumber = newRandomNumber
}

How would I properly format this Swift function to map an array?

I'm trying to build a function with swift that will map an array, divide each value in the array by 3, then spit out a new array. This is what I have so far:
func divideby3Map<T, U>(y: [T], z: T -> U) -> [U] {
let array = [int]()
let divideby3Array = array.map { [y] / 3 }
return dividedby3Array
}
divideby3Map([1,2,3,4,5])
Where T and U are the original array, and the new array being returned respectively, and it's done using generics.
I'm sure this isn't written properly, I'm stuck in terms of the right syntax. For example, since the array being returned is represented by the generic [U], I assume I have to use it somewhere in the array being returned, not sure where though.
When writing a generic function, it’s sometimes easier to approach it in 3 steps: first write the code stand-alone using a specific type. Then write the code as a function, still with a specific type. Finally, change the function to be generic.
The first part, dividing an array by 3, can be done like this:
let a = [1,2,3,4,5]
// map is run on the array of integers, and returns a new
// array with the operation performed on each element in a:
let b = a.map { $0 / 3 }
// so b will be [0,0,1,1,1]
// (don’t forget, integer division truncates)
Note the closure you provide between the { } is an operation that will be applied to each element of the array. $0 represents the element, and you divide it by 3. You could also write it as a.map { i in i / 3 }.
To put this into its own function:
func divideby3Map(source: [Int]) -> [Int] {
return source.map { $0 / 3 }
}
No need to declare a fresh array – map will create one for you. You can then return that directly (you can assign it to a temporary if you prefer, but that isn’t really necessary).
Finally, if you want to make it generic, start by adding a placeholder:
func divideby3Map<T>(source: [T]) -> [T] {
return source.map { $0 / 3 }
}
Note, there’s only a need for one placeholder, T, because you are returning the exact same type you are passed in.
Except… this won’t compile, because the compiler doesn’t know that T is guaranteed to provide two critical things: the ability to divide (a / operator), and the ability to create new T from integer literals (i.e. to create a T with value 3 to divide by). Otherwise, what if we passed an array of strings or an array of arrays in?
To do this, we need to “constrain” T so our function will only accept as arguments types that provide these features. One such protocol we can use to constrain T is IntegerType, which does guarantee these features (as well as some other ones like +, * etc):
func divideby3Map<T: IntegerType>(source: [T]) -> [T] {
return source.map { $0 / 3 }
}
divideby3Map(a) // returns [0,0,1,1,1]
let smallInts: [UInt8] = [3,6,9]
divideby3Map(smallInts) // returns [1,2,3]

Why to use tuples when we can use array to return multiple values in swift

Today I was just going through some basic swift concepts and was working with some examples to understand those concepts. Right now I have completed studying tuples.
I have got one doubt i.e, what is the need of using tuples ? Ya I did some digging on this here is what I got :
We can be able to return multiple values from a function. Ok but we can also do this by returning an array.
Array ok but we can return an multiple values of different types. Ok cool but this can also be done by array of AnyObject like this :
func calculateStatistics (scores:[Int])->[AnyObject]
{
var min = scores[0]
var max = scores[0]
var sum = 0
for score in scores
{
if score > max{
max = score
}
else if score < min{
min = score
}
sum += score
}
return [min,max,"Hello"]
}
let statistics = calculateStatistics([25,39,78,66,74,80])
var min = statistics[0]
var max = statistics[1]
var msg = statistics[2] // Contains hello
We can name the objects present in the tuples. Ok but I can use a dictionary of AnyObject.
I am not saying that Why to use tuples when we have got this . But there should be something only tuple can be able to do or its easy to do it only with tuples. Moreover the people who created swift wouldn't have involved tuples in swift if there wasn't a good reason. So there should have been some good reason for them to involve it.
So guys please let me know if there's any specific cases where tuples are the best bet.
Thanks in advance.
Tuples are anonymous structs that can be used in many ways, and one of them is to make returning multiple values from a function much easier.
The advantages of using a tuple instead of an array are:
multiple types can be stored in a tuple, whereas in an array you are restricted to one type only (unless you use [AnyObject])
fixed number of values: you cannot pass less or more parameters than expected, whereas in an array you can put any number of arguments
strongly typed: if parameters of different types are passed in the wrong positions, the compiler will detect that, whereas using an array that won't happen
refactoring: if the number of parameters, or their type, change, the compiler will produce a relevant compilation error, whereas with arrays that will pass unnoticed
named: it's possible to associate a name with each parameter
assignment is easier and more flexible - for example, the return value can be assigned to a tuple:
let tuple = functionReturningTuple()
or all parameters can be automatically extracted and assigned to variables
let (param1, param2, param3) = functionReturningTuple()
and it's possible to ignore some values
let (param1, _, _) = functionReturningTuple()
similarity with function parameters: when a function is called, the parameters you pass are actually a tuple. Example:
// SWIFT 2
func doSomething(number: Int, text: String) {
println("\(number): \(text)")
}
doSomething(1, "one")
// SWIFT 3
func doSomething(number: Int, text: String) {
print("\(number): \(text)")
}
doSomething(number: 1, text: "one")
(Deprecated in Swift 2) The function can also be invoked as:
let params = (1, "one")
doSomething(params)
This list is probably not exhaustive, but I think there's enough to make you favor tuples to arrays for returning multiple values
For example, consider this simple example:
enum MyType {
case A, B, C
}
func foo() -> (MyType, Int, String) {
// ...
return (.B, 42, "bar")
}
let (type, amount, desc) = foo()
Using Array, to get the same result, you have to do this:
func foo() -> [Any] {
// ...
return [MyType.B, 42, "bar"]
}
let result = foo()
let type = result[0] as MyType, amount = result[1] as Int, desc = result[2] as String
Tuple is much simpler and safer, isn't it?
Tuple is a datastructure which is lighter weight than heterogeneous Array. Though they're very similar, in accessing the elements by index, the advantage is tuples can be constructed very easily in Swift. And the intention to introduce/interpolate this(Tuple) data structure is Multiple return types. Returning multiple data from the 'callee' with minimal effort, that's the advantage of having Tuples. Hope this helps!
A tuple is ideally used to return multiple named data from a function for temporary use. If the scope of the tuple is persistent across a program you might want to model that data structure as a class or struct.

Resources