[Swift 1] Day 4 - Variables, Types, and a Tip Calculator


#1

Read the behind the scenes update here.


#2

The animated slides are great. I also like the code tutorials. Both work pretty well for me.


#3

I’m working to export/upload a bunch more content.

The code stuff too longer than expected.


#4

Oh, ok. I thought you were talking about the previous code exercises.

I have a question about on the size classes. I’ll post in another topic.


#5

Apple’s recommendation is to use the Any x Any and then customize.

I find that a little challenging to think about the UI, when you just want to make an iPhone app.

Look at the new Auto Layout example I did for Day 4. I’ll respond separately about how to handle your situation.


#6

Hi Paul, am I right to say that if you are designing an App purely for iPhone only, you can just choose “compact” format for the Anyw and Anyh?

If designing as an universal app, it is more advisable to use the “open” format for Anyw and Anyh? I suppose the coding is done only once, as compared to objective-c (I remember the coding for iPad and iPhone is done separately but save under the same Xcode file)…


#7

Hi I have my tip calculator laid out so it looks correct on different device and orientations but i get the following errors in the console.

2014-10-14 19:03:45.584 TipCalculator[3750:113047] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don’t want. Try this: (1) look at each constraint and try to figure out which you don’t expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you’re seeing NSAutoresizingMaskLayoutConstraints that you don’t understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

(
    "<NSLayoutConstraint:0x7fbb01d528f0 H:[UILabel:0x7fbb01d52680'$3.00'(90)]>",
    "<NSLayoutConstraint:0x7fbb01d4c820 UILabel:0x7fbb01d51120'$2.00'.leading == UIView:0x7fbb01d53490.leadingMargin>",
    "<NSLayoutConstraint:0x7fbb01d51390 UILabel:0x7fbb01d52680'$3.00'.width == UILabel:0x7fbb01d52f50'$4.00'.width>",
    "<NSLayoutConstraint:0x7fbb01d513e0 H:[UILabel:0x7fbb01d51120'$2.00']-(12)-[UILabel:0x7fbb01d52680'$3.00']>",
    "<NSLayoutConstraint:0x7fbb01d51430 UILabel:0x7fbb01d52680'$3.00'.width == UILabel:0x7fbb01d51120'$2.00'.width>",
    "<NSLayoutConstraint:0x7fbb01d49680 H:[UILabel:0x7fbb01d52680'$3.00']-(12)-[UILabel:0x7fbb01d52f50'$4.00']>",
    "<NSLayoutConstraint:0x7fbb01d440f0 UIView:0x7fbb01d53490.trailingMargin == UILabel:0x7fbb01d52f50'$4.00'.trailing>",
    "<NSLayoutConstraint:0x7fbb01d66600 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fbb01d53490(1024)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbb01d528f0 H:[UILabel:0x7fbb01d52680’$3.00’(90)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.


#8

@iByte, auto layout can be a pain in the a**. When I face the issue you described, I generally have a conflicting constraint in one (or some) of my views. In this scenario, we have to review the constraints for each view, one by one. Unfortunately there is no quick fix.

(If you upload your project, I could check it here in my environment ;))


#9

You could do just the compact size, but only if you’re positive it won’t be made for any other form factor.

It’s a real pain to upscale a Compact x Any to an Any x Any.


#10

Hi,
Tutorial Strings and Characters. When I perform this code:
let letter1: Character = "C"
let letter2: Character = “E”

let comboLetter = letter1 + letter2

The comboLetter is erroring for some reason. Is there a library missing or am I doing something wrong?

Thanks,
Nick


#11

This may have changed as Apple now states (under Concatenating Strings and Characters):

“You can’t append a String or Character to an existing Character variable, because a Character value must contain a single character only.”

So I believe we either need to cast the Character as String or use the .append function as suggested in the documentation.

Like so:

let letter1: Character = "C"
let letter2: Character = "E"

// 1. Casting as String
let comboLetter = String(letter1) + String(letter2)

// 2. Or using String append
var otherComboLetter = String()
otherComboLetter.append(letter1)
otherComboLetter.append(letter2)

Number 1 seems more elegant.

Number 2 will not work with let since let makes the string immutable.


#12

You can also use String Interpolation:

let anotherComboLetter = "\(letter1)\(letter2)"

#13

I would prefer number 1. It’s explicit about what’s happening and concise.

For the most part you won’t be writing code that appends letters. It’s a lot easier to work with Strings. It’s more academic than practical, unless you making a word processor or implementing real-time feedback to typing in a textfield.


#14

Thanks Fernando :smile:


#15

Yes i got the same error… Option 1 worked a treat. These pages are great alongside the course, am really enjoying the course (even with my silly overtired late at night rookie mistakes).

For what its worth from a newbie option 1 was easier to understand than append :smile: