These are chat archives for devslopes/swiftios9

3rd
Nov 2015
JPROFFICIAL
@JPROFFICIAL
Nov 03 2015 01:08
@spentak Does it matter wether or not you define a "get" or just return your private variable? Don't they do the same thing regarding data enclosure? "get" is just labeling it?
Mark Price
@spentak
Nov 03 2015 07:10
In a computed property if all you are doing is retrieving a value then you can just use return. If your computed property is setting a value then you can't use return alone you must use get
@Wrenbjor I'm glad someone can relate to my childhood ;) do you know any other web technologies besides PHP?
itsalexbrown
@itsalexbrown
Nov 03 2015 08:29
Hey is dismissViewControll used for when you show a ViewController via ctr+drag segue…. where as is pop for NavigationViewController?
Wayne
@Wrenbjor
Nov 03 2015 14:16
@spentak Web Development Languages: Python, PHP, Ruby (Rails), Java, Javascript (Old School, JQuery, Angular, Node), HTML, and CSS. I spend most of my time creating RESTful web services. I still can't find the bug.... I am moving on to "Recipez" because I suspect that it's NSUserDefaults, I want to dive into CoreData.
xtr3me
@xtr3me
Nov 03 2015 14:18

How can i have a private var in my super class and set it in my subclass when both classes are not defined in the same file? (Now i get the following error: use of unresolved identifier '_hp')

I think i'm running into the access control level 1 (private entities can only be accessed from within the source file where they are defined.)
ps. i'm currently building the OOP exercise app :smile: :+1:

Wayne
@Wrenbjor
Nov 03 2015 14:55
@xtr3me do you have a github link for that project or can you post a code sample?
xtr3me
@xtr3me
Nov 03 2015 15:00
Hi @Wrenbjor, unfortunately no github as i have my own gitlab instance at home. But this simple code example should show it:
class Character {
    private var _hp = 100
}

class Solider: Character {
  func attemptAttack(attackPower: Int) {
    self._hp -= attackPower //This line gives the error when placing these classes in seperate files
  }
}
Wayne
@Wrenbjor
Nov 03 2015 15:03
So in most programming languages like C++ and Java there is an access control level called protected. That doesnt exist in swift, there is however one called "internal" I would try that. Private vars can only be used in the same file they are defined in.

Swift provides three different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

Public access enables entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use public access when specifying the public interface to a framework.

Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.

Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.

xtr3me
@xtr3me
Nov 03 2015 15:04
ok, i will try that.. but it seems to me that you can access _hp then also from the view controller for example
Wayne
@Wrenbjor
Nov 03 2015 15:06
did you put an accessor on it?
private var _hp: Int

var hp: Int {
   return _hp
}
@xtr3me what lesson is it?
xtr3me
@xtr3me
Nov 03 2015 15:08

lecture 47 (the OOP exercise)

I normally use the accessor as i learned that in this course but found it strange that you can't access private var from a subclass in essence it is the same class.

Wayne
@Wrenbjor
Nov 03 2015 15:20
@xtr3me ok, looking at my code. I have a Character class that is the parent. All the subclasses inherit from that class. The HP is on the super class as well as the method to reduce the hp
import Foundation

class Character
{
    private var _health: Int = 100
    private var _strength: Int = 10

    var strength: Int {
        get {
            return _strength
        }
        set {
            _strength = strength
        }
    }

    var health: Int {
        get {
            return _health
        }
    }

    var isAlive: Bool {
        get {
            if health <= 0 {
                return false
            }
            else {
                return true
            }
        }
    }

    init(startHealth: Int, startStrength: Int) {
        self._health = startHealth
        self._strength = startStrength
    }

    func attemptAttack(attackPower: Int) -> Bool {
        self._health -= attackPower
        return true
    }

}
@xtr3me attemptAttack is the super class
import Foundation

class Enemy: Character {
    var loot: [String] {
        return ["Rusty Dager", "Lint"]
    }

    var type: String{
        return "Grunt"
    }

    func dropLoot() -> String? {
        if !isAlive {
            let rand = Int(arc4random_uniform(UInt32(loot.count)))
            return loot[rand]
        }
        else{
            return nil
        }
    }
}
xtr3me
@xtr3me
Nov 03 2015 15:22

@Wrenbjor i have the same setup but wanted to have a special case for my enemy which gives an extra HP when the attackPower is lower than it's IMMUNE_MAX

Offcourse i can add a function which does that in the superclass but this undermines (my) idea of polymorphism/inheritance

Wayne
@Wrenbjor
Nov 03 2015 15:24
so you are adding a special to a character?
xtr3me
@xtr3me
Nov 03 2015 15:25
@Wrenbjor yeah :smile:
Wayne
@Wrenbjor
Nov 03 2015 15:26
If your special increases the HP of a char then you need to make HP have a set accessor in the parent class. If you dont want to do that then you need to override the HP in the special character class
xtr3me
@xtr3me
Nov 03 2015 15:30
thanks @Wrenbjor
that sounds like a good idea. Hopefully i have some spare time tonight to test this :+1:
Beefycoder
@Beefycoder
Nov 03 2015 21:44
Hey fellas,
Mark Price
@spentak
Nov 03 2015 21:45
Yo!
Beefycoder
@Beefycoder
Nov 03 2015 21:45
I'm only at session 45, where the subject matter is OOP/Proper programming etc., just wondering does the course cover Protocols in Swift 2?
and what is the difference between them & classes. It seems to be a case of whether or not you want an object to be copied or referenced when working with the object
Mark Price
@spentak
Nov 03 2015 21:49
We use protocols in the course many times (ie UITableViewDataSource) but we don't cover creating protocols. That will be in the intermediate course
Beefycoder
@Beefycoder
Nov 03 2015 21:51
OK thanks
Evan Leong
@evanmayo
Nov 03 2015 22:14
Hey @spentak ! In lecture 116 when we work with caching images, we create the NSCache variable as static to be used globally / publicly. Would it be appropriate to store that in the "Constants.swift" file? Thanks!
Mark Price
@spentak
Nov 03 2015 22:14
no
probably better in a service
but its not in a bad place where it is currently
the idea is we only need one instance
itsalexbrown
@itsalexbrown
Nov 03 2015 22:22
with the social network app, what does static let ds = DataService() do within the DataService class? Does that mean there is only one instance?
Mark Price
@spentak
Nov 03 2015 22:23
correct
well
you dont need a reference to it
you just access it like so DataService.sharedInstance().someFunction()
itsalexbrown
@itsalexbrown
Nov 03 2015 22:26
Ah yeah ok I see!
Beefycoder
@Beefycoder
Nov 03 2015 22:26
Hey, wondering if anyone has read Warren Moore's Metal by Example yet?
more specifically, is it worth reading if you are not really familiar with obj-c
Evan Leong
@evanmayo
Nov 03 2015 22:28
got it, thanks @spentak !
Beefycoder
@Beefycoder
Nov 03 2015 22:28
when I started doing this I took an intro course in Obj-c, but swift was already available so I never really bothered with it