ios 如何快速打印数组中对象的值,而不是它的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34350101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print value of object in an array in swift, not its location
提问by Arbab Rizvi
I have a class that contains data on certain entrepreneurs in a separate swift file that is within the same project.
我有一个类,其中包含同一项目中单独的 swift 文件中某些企业家的数据。
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 NSMutableArraywhich contain the instances of the entrepreneur class like this:
在同一个文件中,我有一个函数返回一个NSMutableArray,其中包含像这样的企业家类的实例:
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.swiftfile I create a constant called entrepreneurss and give it a type of the class "Entrepreneur" created above and initialize it like so:
在我的ViewController.swift文件中,我创建了一个名为entrepreneurss 的常量,并给它一个上面创建的类“Entrepreneur”的类型,并像这样初始化它:
let entrepreneuerss:Entrepreneur = Entrepreneur()
I then access one the class methods, the "populateArray" function and try to print the entrepreneurss array like so:
然后我访问类方法之一,“populateArray”函数并尝试像这样打印企业家数组:
entrepreneuerss.populateArray()
print (entrepreneuerss)
The issue is the print functionis printing the location of the object and not the value...something like this: .Entrepreneur: 0x7f88d0e3ecc0>"
问题是打印功能正在打印对象的位置而不是值......像这样:.Entrepreneur:0x7f88d0e3ecc0>”
What do I need to doso 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.
我需要做什么才能返回对象值的数组而不是位置。我希望能够从我的 ViewController.swift 文件访问对象数组并随机选择一个对象并访问其属性。
回答by Micha?l Azevedo
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
对象数组(顺便说一下,我没有看到return
,也许您忘记复制它)。
This method should be a class method, because you don't use any property of the Entrepreneur
object which returns it :
这个方法应该是一个类方法,因为你不使用Entrepreneur
返回它的对象的任何属性:
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 :
其次,在这个方法中,你创建了一个Entrepreneur
对象数组并返回它,但在你的示例代码中,你从不使用这个数组:
// 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 :
为了详细描述您的对象,因为它继承自NSObject
,只需覆盖description
只读属性,以便在您记录对象时自定义文本:
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)`]
回答by Gihan
try
尝试
for element in array {
print(element)
}
回答by NSAnant
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.
我需要做什么才能返回对象值的数组而不是位置。我希望能够从我的 ViewController.swift 文件访问对象数组并随机选择一个对象并访问其属性。
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.
您已经拥有从另一个 swift 文件填充的企业家的所有对象。您可以使用数组中的任何对象并打印它的任何属性。
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)"
}
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
上面的代码将打印企业家的任何对象的名称和公司