Why does the developper examples create an error, both in xcode and in playground?

Hello,

It is probably something very stupid that I should know, but I have not been able to find an expalnation. Neither in this forum, nor in Stackoverflow.

Below an example of code from the developper documentation:

let name = "Marie Curie"
let firstSpace = name.index(of: " ") ?? name.endIndex
let firstName = name[..<firstSpace]
// firstName == "Marie"

When pasting this examples into xcode I get the error below.

(I get the same errors in playground, and if I insert it in a more elaborate context like the body of a button action.

The first and second errors are "Cannot use instance member 'name' within property initializer; property initializers run before 'self' is available"


Any help will be appreciated

Thanks

Lars


Replies

This code alone works well in plmayground (XCode 9.2).


Seem the problem comes from where this is declared. Have you declared a name property in the class or struct definition ?


Can you show more code so that we understand the context.

It works now.

I must have had a currupt configuration.

After upgrading macOS to 10.13.5 and xcode to 9.4.1 everything works as expected.


Thank you for your help, and my apologies for submitting this.

No problem.


Just don't forget to close the thread.