Swift V2 Flashcards

1
Q

What kind of comment is signified by a //:

A

Rich text. You can turn on rendering of comments in XCode which will give it a HTML like appearance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare a new variable?

A

var foobar = 10;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you specify a type of variable?

A

var foobar : String = “foobar”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Do you need to put semicolons in code?

A

No - they are not required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is wrong with the following code?

var foobar =”chicken”;

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you declare a constant?

A

You use the let keyword.

let str2 = “chicken”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is there are a benefit to using constants?

A

Yes, there is a slight memory improvement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does swift using GC?

A

Yes, it uses automated reference counting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are two ways to concatenate the strings:

let firstName = "Cain";
let lastName = "Martin";
A
let name = "\(firstName) \(lastName)"; \\ Interpolation
let name = firstName + " " + lastName; \\ Concatenation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is wrong with the following code?

var a = 5;
a = 5.0;
A

Once you have set the type (by assigning a value of a specific type to the variable) - it cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you declare a float, given that floats and doubles look the same?

A

You have to be explicit - it will default to double.

var c : Float = 10.0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is swift considered type safe?

A

Because you cannot change the type of or put in unexpected types in a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is wrong with the following code?

var a = 10.0;
var b : Float = 4.2;
var c = a + b;
A

You can’t add two numbers of different types, you must cast the value first.

var c = a + Double(b);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What kind of markup does the rich text comments use?

A

Markdown

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would you make a heading with h2’s and h3’s in the rich text comments?

A
/*: 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would you add bulleted list to the rich text comments?

A
/*: This text doesn't show up
 * This is a bullet
 * This is also a bullet
This text just shows up normally
*/

NOTE: Don’t forget the colon after the opening of the comment.

17
Q

What is unusual about the if / else statements of Swift?

A

They don’t require the parenthesis. i.e.

if x == y {
}

NOTE: You can use the parenthesis though if you want

18
Q

How do you declare a boolean?

A

var isABool: Bool;

19
Q

Does swift have a === operator?

A

No - just the standard equality test operator - ==

20
Q

Write a print statement that includes the value of a variable named x using interpolation.

A

print (“The value of x is (x), fool”)