Trouble Converting Code to Swift 3.0, Type Any Has no Subscript [duplicate] - swift

This question already has answers here:
Correctly Parsing JSON in Swift 3
(10 answers)
Closed 6 years ago.
I recently converted my code to Swift 3.0 and a number of errors have shown up. I have viewed other related questions to this specific error, but I have not been able to gain an understanding of what is going on/ what is wrong. I am fairly new to Swift so any explanation is appreciated. Attached is a snippet of my code.

AnyObject has become Any. You probably want this method changed to return [String : Any]. Likely your cellDescriptors array should change to Any as well; this will have some cascading changes but should get you closer to compiling.

Related

Repeatable random data generation in Swift 4 [duplicate]

This question already has answers here:
Swift - Seeding arc4random_uniform? Or alternative?
(2 answers)
ios Swift: looking for a cross compatible method to Java's Random() PRNG that has identical output
(3 answers)
Closed 4 years ago.
I'm writing tests in Swift 4. I want to generate thousands of rows of test input data. I want the test input data to be random.
But I want my tests to be repeatable: the same data needs to be generated for each run of all tests.
How is this achieved in Swift? In Java I've always relied on the Apache commons RandomDataGenerator. I've explored arc4random and drand48, but these don't seem to provide the repeatability I require.
Swift has no native random number generation capabilities. You can call C library functions from Swift, though. Any page on C random number library functions will describe them to you.
For example, you could just seed srand48 with the same constant every time and call drand48 to get your numbers.
(1...3).forEach {_ in
srand48(100)
(1...3).forEach {_ in print(drand48())}
}
/*
// spot the repetition:
0.25105890266514
0.208948404851498
0.940927909958315
0.25105890266514
0.208948404851498
0.940927909958315
0.25105890266514
0.208948404851498
0.940927909958315
*/
Some other C library functions are unavailable from Swift so you'd have to write that part of the code in C. No big deal.

Trying Swift by the book; `for` loop might need updated code [duplicate]

This question already has answers here:
C-style for loop in swift 2 [duplicate]
(3 answers)
Closed 4 years ago.
I recently bought a book about Swift programming - "Swift iOS" by Abhishek Mishra. However, it seems like the code here is outdated, and I might need to figure out what the differences are between the book version and the current version. Here's an example:
for var number = 10; number < 15; number++ {
print ("The value of number is \(number)")
}
What version is used in this book and how should I go about making corrections?
Edit: I wouldn't say this is a duplicate. My question was not strictly about changes to the structure of the for loop; that was just an example. Thank you #everyone for the advice! I think I'll be going through current documentation and adding corrections on sticky notes and such. Good thing I didn't pay too much for the book (there was something wrong with the pages, which stick out at weird angles, so I got a discount, lol)
You can do something like this:
for number in 10..<15 { /// (precision) edited after #rmaddy's comment
print ("The value of number is \(number)")
}

What is the purpose of optionals in Swift? [duplicate]

This question already has answers here:
What is an optional value in Swift?
(15 answers)
Closed 5 years ago.
So I've recently been learning Swift (3.1), and I've had problems with understanding the purpose/practical usage of optionals.
I've researched several sites, and all they talk about is how to use them, not why or when they are used (Sort of why, but not in a way that seems applicable to me). My citations are at the end.
I understand about how its either nil or a value, and how you need to unwrap it's possible value with !, as well as how to create auto-unwrapping optionals.
My main question is just, what are the practical uses of optionals? Apple's Swift handbook said that optionals are the central point of most of Swift's most powerful features, so I feel like it is a concept very worthwhile to fully understand. I fully understand how to write them, I just can't grasp why or when you would use them.
Citations:
https://www.tutorialspoint.com/swift/swift_optionals.htm
http://blog.teamtreehouse.com/understanding-optionals-swift
Thank you for your time.
As per my understanding :
Swift is a safe bound checking language , so it implements it using variable checking whether they are nil or assigned value.
Optionals can be used when you don't know for sure before hand that we would have a value in a specific variable throughout the program.
So if you defined a Text Field & assigned its text to a variable. Maybe user just keeps it blank & you binded that text field's data to a variable.
On runtime it would be empty , for reasons to avoid crash you could define an optional.
In the program when you would access optional value you have to make sure that the variable is present using "if let optional" or explicitly unwrapping the optionals. Explicitly unwrapping optionals are bad move & only should be done when you're sure there is a value present in that optional variable.
Swift uses optionals everywhere.
Disclosure : I'm still new to Swift & this would be first answer. Maybe my answer isn't well put to Stack Overflow's standards.

Error with | in Swift [duplicate]

This question already has answers here:
Swift 2.0 - Binary Operator "|" cannot be applied to two UIUserNotificationType operands
(4 answers)
Closed 7 years ago.
Can someone explain me why it shows error here. I follow the tutorial, but maybe in swift 2, it doesn't true if typing like this.
You are probably following tutorial from older version of Swift. Since its growing really fast, I suggest following the newest tutorials. In Swift 2 you should use collection, not | binary operator. Like:
[UIUserNotificationType.Sound, UIUserNotificationType.Badge]
Your problem is with the first UIUserNotificationType, which is missing (I guess) the .Alert part.

Objective C - Difference Between VARType* vt and VARType *vt [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Placement of the asterisk in Objective-C
What is the differenct between the following:
(Assume we have a class calculator)
Calculator* calc;
Calculator *calc;
Also what is the need for pointers? Is it not possible to omit the star?
There is no difference between those two declarations, you could also do Calculator * calc if you're feeling adventurous.
As far as if you can omit the star, no, you cannot. It is a carry over from C and shows that calc is a pointer to a Calculator, and not a Calculator itself.
There is no difference between the 2 declaration is just an preference for typing.
Every thing on computers uses pointers. You need pointers java, c#, etc use pointers.
No is not posible to omit the star.