Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print value of object in an array in swift, not its location

I have a class that contains data on certain entrepreneurs in a separate swift file that is within the same project.

It looks like this:

class Entrepreneur:NSObject {

var name:String?
var netWorth = (0.0, "")
var company:String?
var summary: [String]
var age: Int?
override init() {
    name = ""
    company = ""
    summary = [""]
    age = 1;

}

In the same file I have a function that returns an NSMutableArray which contain the instances of the entrepreneur class like this:

func populateArray() -> NSMutableArray {

var entreprenuersArray: NSMutableArray = []

//Mark Zuckerberg
let markZuckerBerg = Entrepreneur()
markZuckerBerg.name = "Mark Zuckerberg"
markZuckerBerg.age = 19
markZuckerBerg.company = "Facebook"
markZuckerBerg.netWorth = (35.7, "Billion")

// add mark zuckerberg object to array
entreprenuersArray.addObject(markZuckerBerg)
print (entreprenuersArray)

in my ViewController.swift file I create a constant called entrepreneurss and give it a type of the class "Entrepreneur" created above and initialize it like so:

let entrepreneuerss:Entrepreneur = Entrepreneur()

I then access one the class methods, the "populateArray" function and try to print the entrepreneurss array like so:

   entrepreneuerss.populateArray()
    print (entrepreneuerss)

The issue is the print function is printing the location of the object and not the value...something like this: .Entrepreneur: 0x7f88d0e3ecc0>"

What do I need to do so that I can return an array of the object values and not the location. I want to be able to access the array of object from my ViewController.swift file and randomly select an object and access its properties.

like image 734
Arbab Rizvi Avatar asked Dec 18 '22 21:12

Arbab Rizvi


2 Answers

First, you have a instance method which returns an array of Entrepreneur objects (by the way, I don't see a return, maybe you forgot to copy it).

This method should be a class method, because you don't use any property of the Entrepreneur object which returns it :

class func populateArray() -> [Entrepreneur] {

    var entreprenuersArray:[Entrepreneur] = []

    //Mark Zuckerberg
    let markZuckerBerg = Entrepreneur()
    markZuckerBerg.name = "Mark Zuckerberg"
    markZuckerBerg.age = 19
    markZuckerBerg.company = "Facebook"
    markZuckerBerg.netWorth = (35.7, "Billion")

    // add mark zuckerberg object to array
    entreprenuersArray.append(markZuckerBerg)
    print (entreprenuersArray)

    return entreprenuersArray
}

Then you can have your array by calling :

let array = Entrepreneur.populateArray()

Secondly, in this method, you create an array of Entrepreneur object and returns it, but in your example code, you never use this array :

// Creates a Entrepreneur object with default values
let entrepreneuerss:Entrepreneur = Entrepreneur()

// create an array of entrepreneurs objects, returns it, but never use it
entrepreneuerss.populateArray()

// print some information about the object with default values
print (entrepreneurs)

Instead, you should use the class method and try:

// create an array of entrepreneurs objects, returns it, 
let arrayEntrepreneurs = Entrepreneur.populateArray()

// print some information about the array of Entrepreneurs objects
print (arrayEntrepreneurs)

In order to have a detailed description of your object, since it inherits from NSObject, just override the description read-only property in order to customize the text when you log your object :

override var description : String {
    get {
        return "Entrepreneur : name : `\(name) - company : `\(company)` - summary : `\(summary)` - age : `\(age)`"
    }
}

Thus your print function will return :

[Entrepreneur : name : `Optional("Mark Zuckerberg") - company : `Optional("Facebook")` - summary : `[""]` - age : `Optional(19)`]
like image 64
Michaël Azevedo Avatar answered Dec 21 '22 10:12

Michaël Azevedo


Maybe I am missing something here, but you can just use...

dump(arrayEntrepreneurs)

for Cocco

Swift.dump(arrayEntrepreneurs)
like image 21
Joe Barbour Avatar answered Dec 21 '22 10:12

Joe Barbour