Stuck in an Apple Swift tutorial (SOLVED)

Hi all.


Trying to learn Swift.


Installed Xcode 9.2 (I don't want to take this iMac beyond OS 10.12.6) which, I believe is using Swift 4.


I found this wonderful Apple tutorial, named "Start Developing IOS Apps (Swift)", at https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1.


However, this document is no longer being updated and was last updated in December 2016; so the "project" is probably in Swift, Version 3. I was unable to find a more recent version of this document.


In the section titled "Define Your Data Model", we define a class to contain the data. There is an init (a function? a method?) that causes an error message. I've looked on the Web but could not find an answer.


The error msg is "Initializers may only be declared within a type".


The code looks like this:


Any idea what my next step should be?


Thanks!

>The code looks like this:


Image sharing, if that's what you tried, is prohibited here - see: For Best Results - Read the Label


Instead, use the code editor <> brackets.


Good luck w/the older stuf.

Have you read completely the tutorial ?


Do you speak of the init?() method ?


There in an intentional (pedagogical) errror at the beginning, for which they give the solution later:


Replace this code:

// Initialization should fail if there is no name or if the rating is negative.

if name.isEmpty || rating < 0  {
    return nil
}

With the following:

// The name must not be empty
guard !name.isEmpty else {
    return nil
}

// The rating must be between 0 and 5 inclusively
guard (rating >= 0) && (rating <= 5) else {
    return nil
}


Your init?(name:, photo:, rating:) method should look like this:



init?(name: String, photo: UIImage?, rating: Int) {
   
    // The name must not be empty
    guard !name.isEmpty else {
        return nil
    }
   
    // The rating must be between 0 and 5 inclusively
    guard (rating >= 0) && (rating <= 5) else {
        return nil
    }
   
    // Initialize stored properties.
    self.name = name
    self.photo = photo
    self.rating = rating
   
}

Weird, weird, weird.


[Yes, Claude, the error msg was on the "init" and it said something like "Initializers may only be declared within a type".]


Immediately after the section where I had the error msg, there was a section (in the tutorial) on using the builtin testing facilities. I did this. In the process of doing this, I ran the tests.


Lo and behold! Everything is working!!!


The error msg (about which I posted this initially) HAS DISAPPEARED!!!


So everything is fine.


Don't know why, but it's fine.


Sorry and thanks for the help. (You'll probably see me here often, after I finish the excellent tutorial and attempt stuff on my own!)

Good. Just don't forget to close the thread, by marking the correct answer, which may be yours.

>So everything is fine.


Keep in mind that such tutorials attempt to teach more than just what to put between the margins...

Stuck in an Apple Swift tutorial (SOLVED)
 
 
Q