Coursera - Intro to IOS Development Part 3

Here we go - a quick session while away on a Staycation in RAK!

Continuing with Mark's somewhat pointless (sorry, that is really harsh...) course, let's see what I can get done in 20 minutes...

Unit Tests

So this is something I have never seen before! You need to be on the main file of the app, go to editor and add in unit testing. Then you can test out specific functions or other calculations etc. 

So why, what's the point of unit testing? Right, so the general reason is if someone else e.g. a co-developer is working on your code with you. If they have then changed something, then the unit testing will not work. So one of the main reasons is to protect you from that. 

To be honest, I think it's quite a cool thing to test anyway but with SwiftUI and the 'live' preview of what is on the screen, it's more for the co-development than anything else. If you have a failed unit test, NEVER ship the code!

OK back a day later - ambitious to get much done while on a Staycation! Just to recap from before. I get Unit Testing, makes sense for when working with others. 

Creating Calculations

Well, the way to do this was more complex than I thought. Lots of unwrapping!

if let wageText = hourlyWageLabel.text,let priceText = itemPriceLabel.text {

            if let wage = Double(wageText), let price = Double(priceText) {

                view.endEditing(true)

                resultLabel.isHidden = false

                hoursLabel.isHidden = false

                resultLabel.text = "\(Wage.getHoursForWage(forWage: wage, andPrice: price))"

            }

        }


It does make sense. There are aspects like .isHidden and .endEditing I wouldn't have thought about. By the way, I created a separate function to make the .isHidden being true for the number of hours bit - DRY! I'm sure Mark will get to that too. 


It was cool to see a function within a class being created in the model, rather than as part of the view controller - I don't think I've seen that before. 


Custom Drawing on iOS


So a couple of immediate points here - 


First of all, it is the programmatic approach to making  rectangle - 


let size: CGFloat = 20

        let currencyLabel = UILabel(frame: CGRect(x: 5, y: (frame.size.height / 2) - size, width: size, height: size))


This is put in an override function of 'draw' in the UITextField custom code file. OK, there is some Maths here but the above is pretty general I guess. 


Then we have programming the properties, rather than just selecting them on the storyboard/interface options:


currencyLabel.backgroundColor =  colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 0.2967785494)

        currencyLabel.textAlignment = .center

        currencyLabel.textColor =  colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)

        currencyLabel.layer.cornerRadius = 6.0

        currencyLabel.clipsToBounds = true

        let formatter = NumberFormatter()

        formatter.numberStyle = .currency

        formatter.locale = .current

        currencyLabel.text = formatter.currencySymbol

        addSubview(currencyLabel)


Again, the digital number inputs are all based on the Colour Literal options (comes up as a colour icon on my Xcode).


*Top Tip from Mark - select clip to bounds for anything with rectangles - it won't show the corner radius etc. otherwise. 


Some useful things to takeaway from this, even though they are not all relevant with Swift UI. A key thing with that is I need to accept that I need to know BOTH using Storyboard and using SwiftUI. 


To consolidate, next time I will make my own app project with an idea about suntanning! I will keep the actual app super simple and not be too ambitious for now!


Ok before that, there is a challenge - a km to m converter app. Let's have a look then break it down... Always best to list out what is needed!


Labels for the km/miles

Textfields for each of these

A calculate function and button to work this out


To do that, I will need to borrow from the last project, applying some of the code and ideas. Let's begin with the layout...


MAN THERE IS SO MUCH CODE! The whole picker option thing is just silly on Storyboard. I've looked up two different online explanations and it is mental the amount needed. No thank you, moving on! So, instead I'm going to apply this to my own idea of the suntanning element - there I will delve deep into using TextField, the customising rectangles, the calculation, what being hidden etc. Not with this less interesting example!


Right quiz done - horrible as usual - one question didn't even come up! But I got enough in the end. 


And that's the whole course!


Well it's been a let down to be honest. I had Coursera recommended and should have known better - I have been thorough with finding out what is best for years now after all! My relaunch into Swift has been fine still - I've gleaned a lot back from Storyboard and code in general. 


My consolidation entry tomorrow will be to make the suntanning project, which will take a couple of sittings no doubt. It will just to put into practice things from Weeks 2 to 3 (4 was just the quiz in the end). 


So that's it!

Comments

Popular posts from this blog

Coursera Introduction to Programming in Swift - Part 4