我是 xcode/iOS 开发的 super 新手。我用 objective c 编写了一个教程应用程序,它运行得非常棒。我的下一次尝试是 swift 重写它。
一些非常奇怪的事情正在发生。每次在 xml 文档中找到节点时,“对象”NSMutableArray 都会填充相同的项目。 (而不是追加)。我尝试简单地制作对象:var objects = NSMutableArray() 然后使用 objects.addObject(currentFacility) 但同样的事情发生了。
Log paste 基本上是先登录parser 进入company 节点。然后在 for 循环中,它打印出公司已添加到的对象数组的索引和内容。
我试图删除不相关的代码。但我不确定是什么原因,因此冗长。
class MasterViewController: UITableViewController, NSXMLParserDelegate {
var detailViewController: DetailViewController? = nil
var objects = [Facility]()
var currentFacility = Facility()
var currentValue = ""
var counter = 0
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) {
currentValue = ""
if(elementName == "Facility") {
counter += 1
NSLog("LOG: entered co \(counter) \n")
}
}
func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
//[Code here that correctly catches sub nodes omitted for brevity]
// finished parsing job - add it to the array
if(elementName == "Facility") {
objects.append(currentFacility)
//var desc = currentFacility.description()
for index in 0...(self.objects.count - 1) {
var obPrint = self.objects[index] as Facility
var desc2 = obPrint.description()
NSLog("\(desc2) and index is \(index)")
}
NSLog("LOG: exited co \(counter) \n")
}
}
}
下面的日志粘贴包含 OBJECTS 数组的打印输出,为了便于阅读,删除了一些内容:
parser in DID START document
LOG: entered co 1
ECO FINISHING 5100 INDUSTRIAL BLVD FRIDLEY and index is 0
LOG: exited co 1
LOG: entered co 2
ADVANCED FLEX INC. PLANT 2 3905 CALIFORNIA ST. N.E. COLUMBIA HEIGHTS and index is 0
ADVANCED FLEX INC. PLANT 2 3905 CALIFORNIA ST. N.E. COLUMBIA HEIGHTS and index is 1
LOG: exited co 2
LOG: entered co 3
H. B. FULLER CO. MONARCH DIV. 3900 JACKSON ST. N.E. COLUMBIA HEIGHTS and index is 0
H. B. FULLER CO. MONARCH DIV. 3900 JACKSON ST. N.E. COLUMBIA HEIGHTS and index is 1
H. B. FULLER CO. MONARCH DIV. 3900 JACKSON ST. N.E. COLUMBIA HEIGHTS and index is 2
LOG: exited co 3
more of the same, cut for brevity.
.
.LOG: exited co 10
LOG: entered co 11
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 0
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 1
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 2
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 3
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 4
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 5
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 6
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 7
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 8
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 9
ATLAS MFG. 5250 INDL. BLVD. N.E. FRIDLEY and index is 11
LOG: exited co 11
parser in DID End document
最佳答案
currentFacility
被声明为类本身的属性,并且仅分配给一次(使用其默认值)。此外,Facility
是一个 reference type所以每次你将它附加到数组时,它都会将一个引用附加到同一个实例。
当您更新 currentFacility
的属性时,您正在更新所有附加到数组的属性,因为它们仍然引用相同的东西。
如果你想开始使用一个新的currentFacility
,你应该在找到每个节点后或添加到数组后创建一个新实例:
self.currentFacility = Facility()
关于swift - addObject 发生多次相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25109742/