Swift V2 Flashcards
What kind of comment is signified by a //:
Rich text. You can turn on rendering of comments in XCode which will give it a HTML like appearance
How do you declare a new variable?
var foobar = 10;
How do you specify a type of variable?
var foobar : String = “foobar”;
Do you need to put semicolons in code?
No - they are not required.
What is wrong with the following code?
var foobar =”chicken”;
There is not space after the assignment operator, and there is a space before. These have to be balanced. I.e. if you have a space before, you need one after, and if you don’t have a space before, you shouldn’t have one after.
How do you declare a constant?
You use the let keyword.
let str2 = “chicken”;
Is there are a benefit to using constants?
Yes, there is a slight memory improvement.
Does swift using GC?
Yes, it uses automated reference counting.
What are two ways to concatenate the strings:
let firstName = "Cain"; let lastName = "Martin";
let name = "\(firstName) \(lastName)"; \\ Interpolation let name = firstName + " " + lastName; \\ Concatenation
What is wrong with the following code?
var a = 5; a = 5.0;
Once you have set the type (by assigning a value of a specific type to the variable) - it cannot be changed.
How do you declare a float, given that floats and doubles look the same?
You have to be explicit - it will default to double.
var c : Float = 10.0;
Why is swift considered type safe?
Because you cannot change the type of or put in unexpected types in a variable.
What is wrong with the following code?
var a = 10.0; var b : Float = 4.2; var c = a + b;
You can’t add two numbers of different types, you must cast the value first.
var c = a + Double(b);
What kind of markup does the rich text comments use?
Markdown
How would you make a heading with h2’s and h3’s in the rich text comments?
/*: This text doesn't show up # This is a h1 ## This is a h2 ### This is a h3 This would be normal text */
NOTE: Don’t forget the colon after the opening of the comment.