how to print value of object in an array in swift, not its location
Solution 1
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)`]
Solution 2
try
for element in array {
print(element)
}
Solution 3
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.
You already have the all the objects of entrepreneuers which are populated from your another swift file. you may use any object from the array and print it's any property.
Still you want to print the value of all the objects or all the properties of a object then you need to override the property called "description" into your model class.
add below code to your Entrepreneur class
override var description:String {
return "name :\(self.name) \n company \(self.company)"
}
The above code will print the name and company of any object of Entrepreneur
Arbab Rizvi
Updated on February 09, 2021Comments
-
Arbab Rizvi over 3 years
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.
-
Arbab Rizvi over 8 yearsI made the changes (making the populateArray func a class like so : class func populateArray() -> [Entrepreneur] { var entrepreneursArray: [Entrepreneur] = [] } I then append each object (ex. mark zuckerberg) to the array variable I just created, and I return the entrepreneurArray at the end. In my viewcontroller.swift file I do the following: Let array = entrepreneuer.populateArray() as you mentioned and then I print the array but I just get an array of the object locations not its value..ex: [<.Entrepreneur: 0x7f8c2ae338e0>, <.Entrepreneur: 0x7f8c2ae41510>, <.Entrepreneur: 0x7f8c2ae415
-
Michaël Azevedo over 8 yearsDid you add the description override in your Entrepreneur class ?
-
Arbab Rizvi over 8 yearsHey thanks Michael, it worked! can you please explain why this works? what is this description variable doing?
-
Michaël Azevedo over 8 yearsWhen you try to log or print an object, this string is displayed. The defaut value is the class name, but you can override it and use it as you need :) more info at developer.apple.com/library/mac/documentation/Cocoa/Reference/…
-
Arbab Rizvi over 8 yearsThanks Michael, quick question: when we return the array which includes the name, company etc, I didnt include it in the question but I also have a variable called summary which is an array that holds various facts about mark zuckerberg, will I be able to randomly select a value from this "summary" array that is within the array of entrepreneur?
-
Michaël Azevedo over 8 yearsAdd a
randomFact
readonly String property, and include it in your description return string : var randomFact : String { if summary.count >= 0 { let randomIndex = Int(arc4random_uniform(UInt32(summary.count))) return summary[randomIndex] } return "" } -
rockhammer almost 8 years@MichaëlAzevedo I added your "override var description:..." code into my class definition but xcode gives error "Property does not override any property from its superclass". If I removed the word "override", when I print an element of my array of this class via "print(ToDoListItem[0])" I just get the class name like so "To_Do_List.FirstViewController.ToDoListItem". Did I add it in the wrong place? Thanks
-
rockhammer almost 8 years@MichaëlAzevedo ok I got it. I need to declare my class as subclass of NSObject. Thanks for the code samples