问题描述
我有一个类,该类在同一项目内的单独的swift文件中包含某些企业家的数据.
I have a class that contains data on certain entrepreneurs in a separate swift file that is within the same project.
它看起来像这样:
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;
}
在同一文件中,我有一个函数返回一个 NSMutableArray ,其中包含企业家类的实例,如下所示:
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)
在我的 ViewController.swift 文件中,创建一个称为企业家的常量,并为其提供上面创建的企业家"类的类型,并按如下所示对其进行初始化:
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()
然后我访问一种类方法,即"populateArray"函数,并尝试打印企业家数组,如下所示:
I then access one the class methods, the "populateArray" function and try to print the entrepreneurss array like so:
entrepreneuerss.populateArray()
print (entrepreneuerss)
问题是打印功能正在打印对象的位置而不是值...类似这样的东西:.Entrepreneur:0x7f88d0e3ecc0>"
The issue is the print function is printing the location of the object and not the value...something like this: .Entrepreneur: 0x7f88d0e3ecc0>"
我需要做什么,以便我可以返回对象值而不是位置的数组.我希望能够从ViewController.swift文件访问对象数组,并随机选择一个对象并访问其属性.
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.
推荐答案
首先,您有一个实例方法,该方法返回Entrepreneur
对象的数组(顺便说一句,我看不到return
,也许您忘记复制了).
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).
该方法应该是一个类方法,因为您不使用Entrepreneur
对象的任何属性来返回它:
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()
第二,在此方法中,创建一个Entrepreneur
对象的数组并返回它,但是在示例代码中,您从不使用该数组:
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)
相反,您应该使用class方法并尝试:
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)
为了对对象有详细的描述,因为它是从NSObject
继承的,所以只需覆盖description
只读属性,即可在登录对象时自定义文本:
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)`]
这篇关于如何快速显示数组中对象的值,而不是其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!