This question already has answers here:
Overriding description method in NSObject on swift
(1 answer)
How can I change the textual representation displayed for a type in Swift?
(7 answers)
Closed 5 years ago.
I have an array of type custom class as seen below. It has strings, ints, and double values.
class TrendData: NSObject {
var earlyTime = Date()
var recentTime = Date()
var earlyTimePrice = Double()
var recentTimePrice = Double()
}
I have an array as follows
let dataArray = [TrendData]()
I have filled in 2 values into the dataArray.
Right now when I use a print command as follows
print ("Label: \(dataArray)")
it prints this Label: [<App.TrendData: 0x600000472440>] ** [<App.TrendData: 0x600000472440>]
This is the correct behavior but I want to see all the values within each of these elements in the array. What is the best way to do that? I don't want to explicitly list out each element or even put it into a loop if I can avoid it. I am able to do it, it is just really messy right now. Is there any function or command that does this automatically?
Related
This question already has answers here:
Is Swift Pass By Value or Pass By Reference
(10 answers)
Closed 1 year ago.
Sample code:
struct test {
var itest: Int?;
}
var tests : [test] = [];
var t = test();
tests.append(t)
t.itest = 99;
print(tests[0].itest)
print(t.itest)
produces
nil
Optional(99)
So it seems the append command creates a copy of t and not its reference.
If the array would contain a reference of t, both print outs must be itest=99.
A struct is not an "object" in the way you are probably thinking. It's a value. When you assign a value, it's copied. That's by design.
This question already has answers here:
What is the difference between `let` and `var` in Swift?
(32 answers)
Closed 5 years ago.
I am learning SWIFT. I don't understand one sentence while reading book. What does the sentence below means?:
“Add a constant property called elementList to ViewController.swift
and initialize it with the following element names: let elementList =
["Carbon", "Gold", "Chlorine", "Sodium"]”
does it mean create new class or I have to create struct?
In you situation, you are creating an array of string and store it into a constant variable called elementList. When you use let to create this variable, it means that the value cannot be changed afterwords. So you cannot add or remove element after declaring this array in this way, etc
class ViewController: UIViewController {
var intValue = 1 //This is a property, but it is variable. That means its value can be changed in future.
let doubleValue = 3.14 // This is a property too, but it is constant. That means its value can't be change
// Both `intValue` & `doubleValue` will be in memory till ViewController's existence.
}
IN YOUR CASE:
let elementList = ["Carbon", "Gold", "Chlorine", "Sodium"]
elementList is a array of String, since let keyword denotes that it is a constant property
To Add a constant property called elementList to ViewController.swift and initialize it. IT WOULD LOOK SOMETHING LIKE THIS
class ViewController: UIViewController {
let elementList = ["Carbon", "Gold", "Chlorine", "Sodium"]
//..
}
This question already has answers here:
Why can't I instantiate an empty array of a nested class?
(2 answers)
Why can't I use the short Array constructor syntax when creating an Array of a nested Struct? [duplicate]
(1 answer)
Closed 5 years ago.
struct SomeStruct {
struct AnotherStruct {
var int: Int
}
var int: Int
}
var someArray = [SomeStruct.AnotherStruct]() // error
var anotherArray = Array<SomeStruct.AnotherStruct>() // this works
For the code above, I get a
cannot call value of non-function type '[SomeStruct.AnotherStruct.Type]'. Is this a bug or something that we're stuck with, so we have to use the Array notation?
This is a bug and raise at bugs.swift.org SR349 . you also can't declare empty array of nested class using this.
var someArray = [SomeClass.AnotherClass]() // error
You can use this syntax to declare empty array of struct inside struct like this
var someArray: [SomeStruct.AnotherStruct] = []
This question already has answers here:
Convert Swift string to array
(14 answers)
Closed 6 years ago.
I have a String and I want get an Array of the String's Characters.
I can already do it, like this:
var Carr = [Character]()
for c in s.characters {
Carr.append(c)
}
As you can see, it's not beautiful or efficient.
In Java, I can use char[] sa = s.toCharArray(); to get a char[]. Is there a similarly simple way to do this in Swift?
let charArray = Array(s.characters)
String.characters is a String.CharacterView. It conforms to BidirectionalCollection, which inherits from Collection, and ultimately Sequence.
Because it conforms to Sequence, it can be used in a for loop, as you showed. But also, it can be used in the initializer of Array that takes a sequence.
You are already using the right function:
let yourString = "a string"
let characters = yourString.characters
let count = characters.count
This gives you the collection of characters contained in your string
This question already has answers here:
println dictionary has "Optional"
(2 answers)
Closed 7 years ago.
I'm trying to associate an array numerical data with a column label, so I thought I'd model that with a dictionary that maps a string to an array of floats. I'm trying to populate the dictionary by initializing the key values with empty arrays, but I keep getting optional arrays instead. Here is an example,
var labels = [ "pine", "elm", "palm" ]
var dframe = [String:[Float]]()
for label in labels {
dframe[label] = [Float]()
}
print(dframe.dynamicType) // --> Dictionary<String, Array<Float>>
print(dframe["pine"].dynamicType) // --> Optional<Array<Float>>
I confused about why the initialization [Float]() in the for-loop produces an optional array. If I say [Float]()! in the for-loop, then I get an error saying that I "cannot force unwrap value of non-optional type '[Float]'". Any ideas? I just want a simple, non-optional mapping of strings to arrays of floats. I'm using XCode 7.1.1, and Swift 2.1. Thanks!
When you access a dictionary with:
let x = dframe["pine"]
The compiler didn't know if you pass a valid key to the dictionary, if you didn't, it will return nil. The optional part is not in the array initialization, but in the value search of the dictionary.