Description of Struct not printed in console when executed from Unit Test - ios

I have defined a Struct that adheres to the Printable protocol so I can inspect its value.
struct GridPosition : Hashable, Printable
{
var row: Int
var col: Int
var hashValue: Int {
get {
return ((row + col) * (row + col + 1)) / 2 + col
}
}
var description: String {
return "row: \(row) col: \(col)"
}
}
func == (el: GridPosition, er: GridPosition) -> Bool {
return el.hashValue == er.hashValue
}
When I print the value inside my ViewController the description shows up as expected in the console.
var pos = GridPosition(row: 1, col: 2)
println("value: \(pos)")
Console output:
value: row: 1 col: 2
However, when I try to print the value from my unit test I only get its specifier:
value: GridTests.GridPosition
How can I make my unit test console output more helpful? I'm using Xcode 6.2 (6C131e)

Try explicitly referencing the struct's description property.
println("value: \(pos.description)")
I believe there are other posts on SO where people note that this is a bug, at least in a playground, and explicitly referencing description was the recommended workaround. I haven't seen the bug rear its head in a unit test, but that may be what is going on.

Related

Functions inside Structs - no accessible initialisers error Swift

Following loads of advice from SO in building my first app, I have 2 structs.... 1 for a "WorkoutExercise" and one for a "WorkoutExerciseGenerator".
I'm trying to test out my generator but I'm getting a no accessible initialisers error...
Here's struct 1 :
struct WorkoutExercise {
let name : String
let reps : Int
}
Here's struct 2, with a little test and print at the bottom (which doesn't work) :
struct WorkoutExerciseGenerator {
let name: String
let maxReps: Int
func generate() -> WorkoutExercise {
return WorkoutExercise(
name: name,
reps: Int(arc4random_uniform(UInt32(maxReps))))
}
var test = WorkoutExerciseGenerator(name: "squat", maxReps: 10)
print (test.generate())
}
My thinking here (following a bit of research here https://www.natashatherobot.com/mutating-functions-swift-structs/) is that I'm correctly inserting the parameters for the generator ("squat" and "maxReps:10") so not sure why this wouldn't work? (In this case generating squat + a random number of reps < 10 from "var = test").
After this I'm going to try use an array of exercise names/max rep values to store all my exercises and randomly grab 3 - 6 exercises to create a completely random workout but I think (hopefully) I can work that out if i get this bit
Move the test variable and the print statement out of the struct.
struct WorkoutExerciseGenerator {
let name: String
let maxReps: Int
func generate() -> WorkoutExercise {
return WorkoutExercise(
name: name,
reps: Int(arc4random_uniform(UInt32(maxReps))))
}
}
var test = WorkoutExerciseGenerator(name: "squat", maxReps: 10)
print (test.generate())

Ambiguous use of filter when getting frequency count of characters in a string

I implemented a function to calculate the hamming distance using Swift, it uses the xor operation x ^ y to get the different bits. Then, I convert the result from an Int to a String of 8 characters which is the 8 bit representation of my Xor. However, I am getting the error:
Compile Error: ambiguous use of 'filter'
class Solution {
func hammingDistance(_ x: Int, _ y: Int) -> Int {
let xor = x ^ y //xor: compares bits
let xorBinary = String(xor, radix: 2)
let xor8BitBinaryStr = String(repeating: Character("0"), count: 8 - xorBinary.count) + xorBinary
return xor8BitBinaryStr.filter({ $0 == "1" }).count
}
}
let c = Solution()
print(c.hammingDistance(1, 4)) //prints 2
You can filter like this to avoid confusion for the compiler,
let items = xor8BitBinaryStr.filter({ $0 == "1"})
return items.count
OR
return Array(xor8BitBinaryStr).filter({ $0 == "1" }).count
To solve this, declare the type of the xor8BitBinaryStr before you perform operations on it.
let xor8BitBinaryStr : `data type here` = String(repeating: Character("0"), count: 8 - xorBinary.count) + xorBinary
In Swift 4.0, there are two filter methods on String which only differ by their return type.
One returns String, the other [Character].
If you don't explicitly declare the type of the return you expect, it defaults to String.
Therefore, if you want to get [Character], you need to do something like this:
let chars: [Character] = xor8BitBinaryStr.filter({ $0 == "1" })
return chars.count
EDIT: This was a bug in Swift that was fixed, presumably in 4.1. It was marked Resolved on 11/17/17. See https://bugs.swift.org/browse/SR-5175?jql=text%20~%20%22filter%22

Getting "Argument passed to call that takes no arguments" when trying to use arc4random [duplicate]

I need to generate a random number.
It appears the arc4random function no longer exists as well as the arc4random_uniform function.
The options I have are arc4random_stir(), arc4random_buf(UnsafeMutablePointer<Void>, Int), and arc4random_addrandom(UnsafeMutablePointer<UInt8>, Int32).
I can't find any docs on the functions and no comments in the header files give hints.
let randomIntFrom0To10 = Int.random(in: 1..<10)
let randomFloat = Float.random(in: 0..<1)
// if you want to get a random element in an array
let greetings = ["hey", "hi", "hello", "hola"]
greetings.randomElement()
You could try as well:
let diceRoll = Int(arc4random_uniform(UInt32(6)))
I had to add "UInt32" to make it work.
Just call this function and provide minimum and maximum range of number and you will get a random number.
eg.like randomNumber(MIN: 0, MAX: 10) and You will get number between 0 to 9.
func randomNumber(MIN: Int, MAX: Int)-> Int{
return Int(arc4random_uniform(UInt32(MAX-MIN)) + UInt32(MIN));
}
Note:- You will always get output an Integer number.
After some investigation I wrote this:
import Foundation
struct Math {
private static var seeded = false
static func randomFractional() -> CGFloat {
if !Math.seeded {
let time = Int(NSDate().timeIntervalSinceReferenceDate)
srand48(time)
Math.seeded = true
}
return CGFloat(drand48())
}
}
Now you can just do Math.randomFraction() to get random numbers [0..1[ without having to remember seeding first. Hope this helps someone :o)
Update with swift 4.2 :
let randomInt = Int.random(in: 1..<5)
let randomFloat = Float.random(in: 1..<10)
let randomDouble = Double.random(in: 1...100)
let randomCGFloat = CGFloat.random(in: 1...1000)
Another option is to use the xorshift128plus algorithm:
func xorshift128plus(seed0 : UInt64, _ seed1 : UInt64) -> () -> UInt64 {
var state0 : UInt64 = seed0
var state1 : UInt64 = seed1
if state0 == 0 && state1 == 0 {
state0 = 1 // both state variables cannot be 0
}
func rand() -> UInt64 {
var s1 : UInt64 = state0
let s0 : UInt64 = state1
state0 = s0
s1 ^= s1 << 23
s1 ^= s1 >> 17
s1 ^= s0
s1 ^= s0 >> 26
state1 = s1
return UInt64.addWithOverflow(state0, state1).0
}
return rand
}
This algorithm has a period of 2^128 - 1 and passes all the tests of the BigCrush test suite. Note that while this is a high-quality pseudo-random number generator with a long period, it is not a cryptographically secure random number generator.
You could seed it from the current time or any other random source of entropy. For example, if you had a function called urand64() that read a UInt64 from /dev/urandom, you could use it like this:
let rand = xorshift128plus(urand64(), urand64())
for _ in 1...10 {
print(rand())
}
let MAX : UInt32 = 9
let MIN : UInt32 = 1
func randomNumber()
{
var random_number = Int(arc4random_uniform(MAX) + MIN)
print ("random = ", random_number);
}
In Swift 3 :
It will generate random number between 0 to limit
let limit : UInt32 = 6
print("Random Number : \(arc4random_uniform(limit))")
My implementation as an Int extension. Will generate random numbers in range from..<to
public extension Int {
static func random(from: Int, to: Int) -> Int {
guard to > from else {
assertionFailure("Can not generate negative random numbers")
return 0
}
return Int(arc4random_uniform(UInt32(to - from)) + UInt32(from))
}
}
This is how I get a random number between 2 int's!
func randomNumber(MIN: Int, MAX: Int)-> Int{
var list : [Int] = []
for i in MIN...MAX {
list.append(i)
}
return list[Int(arc4random_uniform(UInt32(list.count)))]
}
usage:
print("My Random Number is: \(randomNumber(MIN:-10,MAX:10))")
Another option is to use GKMersenneTwisterRandomSource from GameKit. The docs say:
A deterministic pseudo-random source that generates random numbers
based on a mersenne twister algorithm. This is a deterministic random
source suitable for creating reliable gameplay mechanics. It is
slightly slower than an Arc4 source, but more random, in that it has a
longer period until repeating sequences. While deterministic, this is
not a cryptographic random source. It is however suitable for
obfuscation of gameplay data.
import GameKit
let minValue = 0
let maxValue = 100
var randomDistribution: GKRandomDistribution?
let randomSource = GKMersenneTwisterRandomSource()
randomDistribution = GKRandomDistribution(randomSource: randomSource, lowestValue: minValue, highestValue: maxValue)
let number = randomDistribution?.nextInt() ?? 0
print(number)
Example taken from Apple's sample code: https://github.com/carekit-apple/CareKit/blob/master/CareKitPrototypingTool/OCKPrototyper/CareKitPatient/RandomNumberGeneratorHelper.swift
I'm late to the party 🤩🎉
Using a function that allows you to change the size of the array and the range selection on the fly is the most versatile method. You can also use map so it's very concise. I use it in all of my performance testing/bench marking.
elements is the number of items in the array
only including numbers from 0...max
func randArr(_ elements: Int, _ max: Int) -> [Int] {
return (0..<elements).map{ _ in Int.random(in: 0...max) }
}
Code Sense / Placeholders look like this.
randArr(elements: Int, max: Int)
10 elements in my array ranging from 0 to 1000.
randArr(10, 1000) // [554, 8, 54, 87, 10, 33, 349, 888, 2, 77]
you can use this in specific rate:
let die = [1, 2, 3, 4, 5, 6]
let firstRoll = die[Int(arc4random_uniform(UInt32(die.count)))]
let secondRoll = die[Int(arc4random_uniform(UInt32(die.count)))]
Lets Code with Swift for the random number or random string :)
let quotes: NSArray = ["R", "A", "N", "D", "O", "M"]
let randomNumber = arc4random_uniform(UInt32(quotes.count))
let quoteString = quotes[Int(randomNumber)]
print(quoteString)
it will give you output randomly.
Don't forget that some numbers will repeat! so you need to do something like....
my totalQuestions was 47.
func getRandomNumbers(totalQuestions:Int) -> NSMutableArray
{
var arrayOfRandomQuestions: [Int] = []
print("arraySizeRequired = 40")
print("totalQuestions = \(totalQuestions)")
//This will output a 40 random numbers between 0 and totalQuestions (47)
while arrayOfRandomQuestions.count < 40
{
let limit: UInt32 = UInt32(totalQuestions)
let theRandomNumber = (Int(arc4random_uniform(limit)))
if arrayOfRandomQuestions.contains(theRandomNumber)
{
print("ping")
}
else
{
//item not found
arrayOfRandomQuestions.append(theRandomNumber)
}
}
print("Random Number set = \(arrayOfRandomQuestions)")
print("arrayOutputCount = \(arrayOfRandomQuestions.count)")
return arrayOfRandomQuestions as! NSMutableArray
}
look, i had the same problem but i insert
the function as a global variable
as
var RNumber = Int(arc4random_uniform(9)+1)
func GetCase(){
your code
}
obviously this is not efficent, so then i just copy and paste the code into the function so it could be reusable, then xcode suggest me to set the var as constant so my code were
func GetCase() {
let RNumber = Int(arc4random_uniform(9)+1)
if categoria == 1 {
}
}
well thats a part of my code so xcode tell me something of inmutable and initialization but, it build the app anyway and that advice simply dissapear
hope it helps

Swift3 Random Extension Method

I was using this extension method to generate a random number:
func Rand(_ range: Range<UInt32>) -> Int {
return Int(range.lowerBound + arc4random_uniform(range.upperBound - range.lowerBound + 1))
}
I liked it b/c it was no nonsense, you just called it like this:
let test = Rand(1...5) //generates a random number between 1 and 5
I honestly don't know why things need to be so complicated in Swift but I digress..
So i'm receiving an error now in Swift3
No '...' candidates produce the expected contextual result type 'Range<UInt32>'
Would anyone know what this means or how I could get my awesome Rand function working again? I guess x...y no longer creates Ranges or x..y must be explicitly defined as UInt32? Any advice for me to make things a tad easier?
Thanks so much, appreciate your time!
In Swift 3 there are four Range structures:
"x" ..< "y" ⇒ Range<T>
"x" ... "y" ⇒ ClosedRange<T>
1 ..< 5 ⇒ CountableRange<T>
1 ... 5 ⇒ CountableClosedRange<T>
(The operators ..< and ... are overloaded so that if the elements are stridable (random-access iterators e.g. numbers and pointers), a Countable Range will be returned. But these operators can still return plain Ranges to satisfy the type checker.)
Since Range and ClosedRange are different structures, you cannot implicitly convert a them with each other, and thus the error.
If you want Rand to accept a ClosedRange as well as Range, you must overload it:
// accepts Rand(0 ..< 5)
func Rand(_ range: Range<UInt32>) -> Int {
return Int(range.lowerBound + arc4random_uniform(range.upperBound - range.lowerBound))
}
// accepts Rand(1 ... 5)
func Rand(_ range: ClosedRange<UInt32>) -> Int {
return Int(range.lowerBound + arc4random_uniform(range.upperBound + 1 - range.lowerBound))
}
A nice solution is presented in Generic Range Algorithms
(based on How to be DRY on ranges and closed ranges? in the swift-users mailing list).
It uses the fact that both CountableRange and CountableClosedRange
are collections, and in fact a RandomAccessCollection.
So you can define a single (generic) function which accepts both open and closed
integer ranges:
func rand<C: RandomAccessCollection>(_ coll: C) -> C.Iterator.Element {
precondition(coll.count > 0, "Cannot select random element from empty collection")
let offset = arc4random_uniform(numericCast(coll.count))
let idx = coll.index(coll.startIndex, offsetBy: numericCast(offset))
return coll[idx]
}
rand(1...5) // random number between 1 and 5
rand(2..<10) // random number between 2 and 9
but also:
rand(["a", "b", "c", "d"]) // random element from the array
Alternatively as a protocol extension method:
extension RandomAccessCollection {
func rand() -> Iterator.Element {
precondition(count > 0, "Cannot select random element from empty collection")
let offset = arc4random_uniform(numericCast(count))
let idx = index(startIndex, offsetBy: numericCast(offset))
return self[idx]
}
}
(1...5).rand()
(2..<10).rand()
["a", "b", "c", "d"].rand()
You could rewrite Rand() to use Int if that is your primary use case:
func Rand(_ range: Range<Int>) -> Int {
let distance = UInt32(range.upperBound - range.lowerBound)
return range.lowerBound + Int(arc4random_uniform(distance + 1))
}
Or as kennytm points out, use Rand(1..<6)

Swift error, cannot convert type Int to int 16

This function compares the number with the sum of the cubes of the components of this number. For example abc=a^3 + b^3 + c^3. There is an error converting, please help.
func triKuba ( i:Int16, k:Int16, var array:[Int16]=[] ) ->Int16{
for var i=100;i<1000; i++ {
array.append(Int16(i))
if array[i] == pow(array[i]/10) + pow(array[i]/100) + pow(array[i]%10) {
return array[i]
} else {
return 0
}
}
}
triKuba(0, k: 0)
next error in line around method pow 'Cannot invoke pow with argument list of type Int16' if I understood correctly, method pow is a^3
I strongly suspect this is what you are looking for:
func arithmeticRoot3(var value: Int) -> Int {
var result = 0
while value > 0 {
let digit = value % 10
result += digit * digit * digit
value /= 10
}
return result
}
func triKuba() -> [Int] {
return (100...999).filter() {
$0 == arithmeticRoot3($0)
}
}
print(triKuba()) // [153, 370, 371, 407]
Rather than just solving your issue, I'm going to explain what the problem is, and why it throws an error. With this information you should be able to fix the issue (and similar ones in the future!).
In Swift, you can't always do things like multiply an Int with a Float, or return an Int16 from a Double type return function. To you, it might be obvious that 'casting' the variable to the intended type would be fine - but the compiler doesn't know that.
If you're sure it will be safe, you can 'cast' variables to the required type:
Int(int16Variable) // int16Variable has been casted to an 'Int'.
The method you posted had several syntax issues. I fixed them. Working method below:
func triKuba ( i:Int16, k:Int16, var array:[Int16]=[] ) ->Int16{
for var i=100;i<1000; i++ {
array.append(Int16(i))
if Double(array[i]) == pow(Double(array[i])/10, 3.0) + pow(Double(array[i]/100), 3.0) + pow(Double(array[i]%10), 3.0) {
return array[i]
} else {
return 0
}
}
}
Error you were getting was about missing cast from Int to Int16(i)
in your for loop you are declaring a new i variable which is implied to be of type Int as opposed to Int16. (Due to shadowing this is the variable being used in your function body, the parameter i you pass in is never being used.
I would advise either changing the type for your function parameters to be of type Int like so:
func triKuba (var i: Int, var k: Int, var array: [Int]=[] ) -> Int {
or cast your Int16 variables to Ints like so:
array.append(Int16(i))

Resources