Swift 2.0 stuff


#1

Before you waste some time with the latest Apple 2.0 , here is something I encountered just now after updating to Xcode 7.1, ios 9.1 …


#2

thanks for sharing!

Is there documentation on the change or suggestion from Apple?


#3

I didn’t see any direct from Apple, but they are saying that paths shouldn’t be used as they don’t actually verify the existence of the object, where URLs do.
It’s actually funny it wasn’t letting me compile, I created new functions and kept the old one commented.
I compiled successfuly, then just for the fun of it I uncommented the old ones and changed their names but compiled without a problem. I guess it might’ve succeeded because I never used the new functions anywhere within the code…who knows. :slight_smile:


#4

I’ll have to give it a try – downloading the new Xcode 7 (non-beta) now.


#5

I found that I can have all installed at the same time.
Currently I have 6.x , 7.1 GM and 7.1 beta :slight_smile:


#6

Since updating to Swift 2 and Xcode 7, I can’t get the “showAlert” function to work. I have tried changing everything that I see but must be missing something simple. Any thoughts would be helpful and appreciated. The error I am getting is “Binary operator ‘<=’ cannot be applied to operands of type ‘String’ and ‘Int’” at the 4 “if newConstant”
(PS this all worked pre Swift2 and Xcode 7)

@IBAction func showAlert() {

   var title: String

    let newConstant = enterHoursTextField.text!
  
    
        
 
       if newConstant <= 24 {
        
        title = "You're sleeping too much, click on the 'Facts' button."
    }
    
    if newConstant <= 9 {
        
        title = "Thats good, a good nights sleep is from 7 to 9 hours. Click on the 'Facts' button."
    }
    
    if newConstant <= 6 {
        
        title = "You should go back to bed and try to get at least 7 hours of sleep. Click on the 'Facts' button."
    }
    
        
    if  newConstant >= 25 {
       
        title = "The number must be 24 or less"
    }

  
   
        let alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Close", style: .Default, handler: nil))

       self.presentViewController(alert, animated: true, completion: nil)

#7

Not sure why this was working before :slight_smile:
You are trying to see if a parameter is <= 24 , but you parameter is not necessary a number.
You have to convert to Int first…
So something like :smile:

if Int(newConstant) <= 24 …

Even better create a new one first…

If let newNumberConstant = Int(newConstant) {

if newNumberConstant <= 24… blabla…

}


#8

@drewgost

I’m not sure how that would work, without converting it to a number first. You can use the new initializers in Swift 2 to convert String input into numbers (Double, Int, etc.)

I reworked your code a bit using a few things (along with some comments).

  1. Use the if/if else/else to control the message better, your original logic didn’t always work correctly. If/else allows you to control it so that only one case gets matched.

  2. You should use the new Double() initializer to convert, it’s safest to wrap this in an if/let statement to unwrap valid numeric values (and you can display an error when incorrect).

     @IBAction func showAlert() {
         
         let title: String
         
         // Convert to a double number to perform logic checks
         if let newConstant = Double(enterHoursTextField.text!) {
             
             // You want to make sure it's within a range, use the && for AND
             //  or the || for OR
             // The if/else if/else structure lets you handle all cases
             if newConstant >= 10 && newConstant <= 24 {
                 
                 title = "You're sleeping too much, click on the 'Facts' button."
             } else if newConstant <= 9 && newConstant >= 7 {
                 
                 title = "Thats good, a good nights sleep is from 7 to 9 hours. Click on the 'Facts' button."
             } else if newConstant <= 6 {
                 
                 title = "You should go back to bed and try to get at least 7 hours of sleep. Click on the 'Facts' button."
             } else { // if newConstant >= 25 || newConstant < 0 {
                 
                 title = "The number must be between 0 and 24"
             }
             
         } else {
             // invalid user input if Double() conversion fails (nil is returned)
             title = "Invalid user input, enter a number!"
         }
         
         let alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
         
         alert.addAction(UIAlertAction(title: "Close", style: .Default, handler: nil))
         
         self.presentViewController(alert, animated: true, completion: nil)
    
     }

#9

Wow, thank you Paul I have been going crazy for months :scream: It works perfect!! I will read the link above completely, so I can better understand the changes that were needed. I feel the part I need to learn is more of the Language, I think it is great when you add the links to the subject we are going over.

Thanks,

Drew


#10

Hi rave, I think my nephew may have changed the code, because when I posted it I felt it looked odd as well, unfortunately I did not save it anywhere else. That could be the only reason that you and Paul S are looking at it as being??. If you go to the App store and download it “Sleep Smarter”(it’s free) you can see it worked great before 9.1 and Xcode 7 bla bla bla.
But thanks for the input.
If you remember it was you that helped me understand how to make this work to get it into the app store. :grinning:


#11

My new Super Easy iPhone Apps course is going to have new code and lessons around Swift 2.

I’m working on blogging more, so I can link to better content. There is a lack of online resources outside of Apple’s documentation on Swift 2.


#12

I agree with that no resources. I finished the Days Old Calculator course and excited for this weekend. We are still on course for the 30th?


#13

Yes! I’ve got over 1 hour of content, and I’m recording more this week.

Any interest in PDF handouts? Here’s a rough draft from Lesson 3.