This question already has answers here:
Confused about Swift Array Declarations
(2 answers)
Closed 5 years ago.
I was wondering what is the better way to declare variables?
private var arrayOfStrings: [String] = []
or
private var arrayOfStrings = [String]()
What are the advantages/disadvantages of either solution?
Thanks
As noted here there are little differences in the 2 ways.
Using type annotation you declare which type the variable is, while not using it you let Swift infer the type of the variable.
Almost always Swift will infer the correct type. Be careful:
As you can encounter situations like this in which Swift will have a decision to take. Read the full documentation here.
Related
This question already has answers here:
Kotlin-idiomatic way to add an item to a list in a map
(5 answers)
Closed 1 year ago.
Recently I find myself dealing with maps of <String, List<...>> like this:
val bc = mutableMapOf<String, MutableSet<Int>>()
if (bc[question] == null)
bc[question] = mutableSetOf()
bc[question]!!.add(line)
Isn't there a better way to do this? I've tried
bc[question]?.add(x) ?: = mutableSetOf(x)
But that won't work. I've looked here and on the Kotlin null-safety page
and other similar questions here but didn't find anything. I'm still some-what new to Kotlin.
I do it using run function to assign mutableSetOf you can try it
bc[question]?.add(line) ?: run { bc[question] = mutableSetOf(line) }
This question already has answers here:
ArrayList<String>() vs arrayListOf<String>()
(5 answers)
Closed 3 years ago.
I searched the difference between arrayListOf and ArrayList.
so I understand arrayListOf is function, and ArrayList is class.
but I don't understand the exact difference using them.
As described in the documentation arrayListOf is a function that creates an ArrayList instance. If I understand it correctly it serves the purpose of determining the generic type from the passed input values and it's a convenience function.
This question already has answers here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
(4 answers)
Closed 4 years ago.
I sometimes see statements like somevariable.value?.add() What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
Kotlin treats null as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value is of a "nullable type", such as MutableList? or Axolotl?. A MutableList cannot be null, but a MutableList? might be null.
Normally, to call a function on an object, you use a ..
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.. Then, one of two things will happen:
If the value is null, your function call is ignored, and null is the
result
If the value is not null, your function call is made as normal
So, in your case:
If somevariable.value is null, the add() call is skipped
If somevariable.value is not null, the add() call is made on whatever somevariable.value is
I'm a beginner to Swift and I'm trying to run Swift 2.3 code in Xcode 8.0
var result: SecTrustResultType = 0
SecTrustEvaluate(trust, &result)
Error message: Cannot convert value of type 'int' to specified type 'SecTrustResultType'
I asked this question before and it was marked a duplicate, but the link to the similar question didn't exactly help me.
Cannot convert value of type 'int' to specified type 'SecTrustResultType'
SecTrustResultType is likely an enum. Check the Swift language book (freely available on the AppStore) to see how you initialise an enum. That's a basic language feature that you must learn, so it's pointless to tell you for this specific case.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Method overloading in Objective-C?
Is method overloading not possible.
I have two functions with the same name.
When declared like the below i'm etting errors.
-(RS232Msg*)CreateMessage:(REMOTE_MESSAGE_ID) nMessageNumber;
-(RS232Msg*)CreateMessage:(const uint8_t*) szMessageName;
when declared -(RS232Msg*)CreateMessage:(const uint8_t*) szMessageName; i'm not getting any errors.
I also have two functions as the same name with different return type and argument.But its working fine and there is no error in its declaration.
Why is it so?
No, method overloading is not possible in C, and therefore not possible in Objective-C (since Objective-C is a superset of C). If you'd like to use those two methods, you'll have to change their names. I would suggest the following:
- (RS232Msg *)createMessageWithMessageID:(REMOTE_MESSAGE_ID)nMessageNumber;
- (RS232Msg *)createMessageWithName:(const uint8_t*)szMessageName;