# SearchResult class

**URL:** <https://forums.kodeco.com/t/searchresult-class/64441>\
**Category:** iOS Apprentice\
**Created:** [March 13, 2019, 10:47pm UTC](https://forums.kodeco.com/t/searchresult-class/64441 "2019-03-13T22:47:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jiang/32/79544_2.png) [@jiang](https://forums.kodeco.com/u/jiang)\
**Post date:** [March 13, 2019, 10:47pm UTC](https://forums.kodeco.com/t/searchresult-class/64441/1 "2019-03-13T22:47:39Z")

</div>

Hi,

When you define SearchResult class, why you initialize some optional variables to empty strings? The code is below:

> **var** artistName: String? = “”  
> **var** trackName: String? = “”  
> **var** kind: String? = “”  
> **var** trackPrice: Double? = 0.0  
> **var** currency = “”  
> **var** imageSmall = “”  
> **var** imageLarge = “”  
> **var** trackViewUrl: String?  
> **var** collectionName: String?  
> **var** collectionViewUrl: String?  
> **var** collectionPrice: Double?  
> **var** itemPrice: Double?  
> **var** itemGenre: String?  
> **var** bookGenre: [String]?

```
var name: String {
    return trackName ?? ""
}

```

I don’t see the point to the initialization. When parse the json data, if artistName is not a key in the dictionary, then the artistName will be given nil; if artistName is a key, then the artistName variable will be given a string value. Is that necessary to initialize the artistName at declaration?

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [April 1, 2019, 3:03am UTC](https://forums.kodeco.com/t/searchresult-class/64441/2 "2019-04-01T03:03:25Z")

</div>

@jiang Thanks very much for your question!

You make a very strong point here, and I do agree that it is unnecessary to add default values. Having said that, one reason why a default value is provided is to ensure that a value for the property is always given, and thus, the key always appears in the output. I personally have worked with JSON and often times, if the key value is nil, the property will not appear in your display.

I hope this helps!

All the best!

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [August 27, 2019, 2:47pm UTC](https://forums.kodeco.com/t/searchresult-class/64441/3 "2019-08-27T14:47:39Z")

</div>

Closed on August 27, 2019, 2:47pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
