Programming in Swift · Methods | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/44

When I walk through the code examples presented, Xcode is reporting the following error when I try to define the Weekday enum to conform to the CaseIterable protocol

enum Weekday: CaseIterable {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
}

*** Type ‘Weekday’ does not conform to protocol ‘RawRepresentable’ ***

When I open the PS_5-07_End.playground to see how it defines this, I see the same error (and a whole bunch more, probably stemming from the first).

Is this because I don’t have a compatible simulator installed on my version of Xcode?

I am running Xcode 9.4.1 (9F2000) with the iOS 11.3 simulator

@synerjohnny This is a Swift 4.2 feature, so you need to download and install Xcode 10 in order to run the playground properly. Please let me know if you have any other questions or issues about the whole thing. Thank you!

1 Like

Thanks for your reply. :slight_smile:

FYI - For anyone looking to determine the version of Swift they are running, I found the following command to run in the terminal on StackOverflow:

$ swift -version

In my case, the result looked like this:

Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

Target: x86_64-apple-darwin17.7.0

So yes, I was running the older version of Swift.

Hey, i am on Structures part, is it okay that i don’t understand some of the concepts in structures? I do get how it works, but with some parts of them i really confused. You guys, explain really quick and do not explain how some processes work. My question, will be it okay if i continue to the next parts ?

@didar490 Please let us know what you don’t understand exactly about the whole thing when you get a chance. Thank you!

Can you recommend some additional tools to increase the understanding of code?
Right now I totally sure you need to have strong mathematics
and logic skills to create the code like what we see in this lesson.

I understood nothing in this lesson, but don’t want to give up.
Maybe you have additional course, which is more easy.

1 Like

@ollybess Please check out our Swift Apprentice book when you get a chance:

https://store.raywenderlich.com/products/swift-apprentice

I hope it helps!

You might also want to try going through the Your First iOS App course. You actually build an app in that course, and it may help you to see code used in a more concrete context.

Best of luck!

Yes, I finished “Your first iOS App” several months ago. It was much easier.

I’m gonna try the book. Thanks.

Can you please explain a couple of things that aren’t clear to me? 1. Why did you use ‘self’ on line 36 in the placeholder Weekday? What is this referring back to exactly? 2. On line 37 you changed the type of (dayCount) to type Int. As someone who is new to Swift, how can you tell that these were different types? It is not clear to me from the error that is generated ( ‘+’ is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics.) that there is a type mismatch. Is there a way to check the type of indexOfToday and dayCount?

  1. The today part of indexOfToday is self. It’s whatever day the enumeration instance represents.

allCases is a property of the Weekday type, so you have to use Weekday first in an instance method in order to use allCases.

  1. In general, people (including Apple) use Int to represent all integers. If you expect to be working with integers in someone else’s code, even if the numbers can’t be negative, you’ll probably be dealing with Int. That prevents anyone having to perform conversions between integer types, but it also allows for crashes and other nonsense to occur when only positive numbers are expected. How you deal with that upside and downside is up to you when you write your own functions.

Is there a way to check the type of indexOfToday and dayCount?

Yes! Option-clicking on just about anything in Swift will give you some handy information about it.

Unlike the example in the video [3:20] ,
I had to do an optional binding in order to get rid of the errors .

My method inside the Weekday enum ended up like this :

mutating func advance(by dayCount: UInt) {
    let indexOfToday = Weekday.allCases.firstIndex(of: self)
    
    if let indexOfToday = indexOfToday {
        let indexOfAdvancedDay = indexOfToday + Int(dayCount)
        
        self = Weekday.allCases[indexOfAdvancedDay % Weekday.allCases.count]
    }     
}

:point_right: When you OPTION click on the .firstIndex method , you learn that the method returns an optional Int .

Did the Swift Apprentice book :orange_book: help ?

You missed the force unwrap that we used at the end of this line:

let indexOfToday = Weekday.allCases.firstIndex(of: self)!

Force unwrapping is a more meaningful choice here, because there is no possibility of indexOfToday being nil.

:+1: Ok .

I am going to get into the habit of unwrapping everything with the optional bind instead of the bang :firecracker: operator .

It is not that, is it? Going through the first lot of videos does not help one iota. Let’s face it, the explanations provided are just incompetent. It is beyond me how anybody could sign off your videos for tutorial purposes. As I said earlier, get your customer segment right. Because right now, all you have achieved is stolen my time. Thanks

@jezza Please let us know what you don’t understand exactly when you get a chance. Thank you!

I had trouble following too at first but I found it very helpful to look at Apple’s documentation for each of the subjects. I don’t mean the actual documentation for a function or a call but, let’s say the topic of properties in general. What these short tutorials cover is most of the coding basics you will stumble upon in your professional work and they tend to go fast. They can be overwhelming but each time you go through them you will notice, or understand, something new.

1 Like

At 13:10 Jessie says “and this doesn’t make sense” when talking about creating an instance of the Mathematics struct. Why did he say that? Is it because the Mathematics struct only had a type method and nothing else and so there’s no point creating an instance of a Type that doesn’t offer you anything more than the Type method which the Type was already offering you without you having to create an instance in the first place?

1 Like