I'm trying to initialize an object and I get this error: Variable used within its own initial value
Here is my code:
Why can't I do it like this?
The others answers are correct. I would like to add a few suggestions here because I see many very ugly things in you code :[
So name your class Entity instead of entity
Why does Entity extend NSObject? Do you have a valid reason to do it?
I see lots of public in you code. Why? Are you creating a library? Because probably there's no need to make public all that stuff.
If Entity is simply a container for a bunch of data it should be a struct, not a class. You'll get better performance.
Too often I see code where the String type is used in place of many others types (such as Int, NSDate, Bool, ...). Why? This makes extremely difficult to deal with that value every time you need to perform some action/check strictly related the real type (like adding numbers, comparing dates, etc...)
What does it mean when created is an empty string? If you are using the empty String to indicate the absence of a value you should use nil instead (making of course the property an optional type).
Hope this helps :)
public the property becomes internal then visibile to your entire project. This should be enough to access it. Anyway don't manually write getters/setters. 4) When your API sends you no time set, just set the related property to nil: it's a better way to express no value then the text no time set.You can't give a variable the same name as a type: the compiler's getting confused and it thinks your reference to entry the type is referring to entry the new variable, hence the warning.
The best solution is to use CamelCase names for types and llamaCase names for variables, as recommended by Apple in "The Swift Programming Language". For example:
public var entry = Entry()
and
public class Entry: NSObject {
(Note also that you needn't explicitly state the type of the entry variable, as it's easily deduced from its value.)
Change the name of the instance because you can't use the same name as the class. In addition you should change the name of the class to uppercase where it becomes easier to differ class name and instance
var levelEntry = Entry()
public class Entry: NSObject. Having said that, looking at Entry class it should probably be a struct.