3

I'm trying to initialize an object and I get this error: Variable used within its own initial value

Here is my code:

enter image description here

Why can't I do it like this?

11
  • 2
    Class names start with an uppercase letter. Instance names start with a lowercase letter. There is a reason for this. public class Entry: NSObject. Having said that, looking at Entry class it should probably be a struct. Commented Sep 23, 2015 at 13:03
  • 1
    Why you specify public explicitly everywhere ? Commented Sep 23, 2015 at 13:07
  • @Fogmeister, from memory consumption perspective, Entry should rather be a class (as that is of reference type) then a struct (as that is a value type and gonna be copied all the way around when new "var/let references" to it appear). Also, I guess that Entry is supposed to be a subclass of NSObject (for the sake of KVO usage or whatever else). Commented Sep 24, 2015 at 15:00
  • @ArthurGevorkyan I disagree. Memory consumption of this data is minimal to the point of being negligible. Using reference types can be more complex as you can get into situations where several parts of he app can be mutating data at the same time. It becomes harder to have a source of the "truth". With value type objects this isn't an issue and parts of the app that do need to mutate can be written with the intention of mutating and passing mutated data back etc... That way there is always one source of the truth. Commented Sep 24, 2015 at 15:04
  • @Fogmeister, as I said, it's all about memory consumption solely. I haven't been speaking about mutability at all. Your point of view is as good as mine, it just relates to a completely different aspect. Commented Sep 24, 2015 at 15:08

4 Answers 4

9

The others answers are correct. I would like to add a few suggestions here because I see many very ugly things in you code :[

1. Type names should be UpperCamelCase

So name your class Entity instead of entity

2. Extending NSObject...

Why does Entity extend NSObject? Do you have a valid reason to do it?

3. Visibility

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.

4. Struct instead of Class

If Entity is simply a container for a bunch of data it should be a struct, not a class. You'll get better performance.

5. The "created" property declared as String? Seriously?

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...)

6. Everything initialised to "", why?

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

Some answer to your points: 2 - I pass the object to an external library that only accept NSObject, not struct. 3 - Easy access to data in object, is it better to make get/set methods? 4 - same as 2 5 - I get Data from an API and sometimes i get "no time set" instead of a datetime for published for example.
2) Ok, then you are right 3) If your remove 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.
5

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.)

Comments

2

You just have a collision of the name of the entry variable reference and the entry class. Just rename the entry class: make it "Entry" for instance.

Comments

1

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()

1 Comment

IMHO, naming classes in a groovy way (upper camel case) is a better solution compared to making the other names deal with awkward class names :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.