追加时替换URL

时间:2017-05-12 13:32:55

标签: ios swift swift3

我的代码有问题,我认为是逻辑问题,但是Master可以睁开眼睛......程序在执行时复制了url3。我真的很喜欢,需要一些帮助。我只发布了游乐场代码,但如果有人希望我可以共享github代码。

import UIKit

class AppPhotos: NSObject {
    var data_consulta: String?
    var apps: [App]?
    var type: String?

    static func testePhotos() {
        var fotos = [AppPhotos]()

        let teste = true
        if teste {
            let appPhotos = AppPhotos()
            var apps = [App]()
            let app = App()

            app.url = NSURL(string: "https://url1") as URL?
            apps.append(app)
            appPhotos.data_consulta = "1"
            appPhotos.apps = apps
            fotos.append(appPhotos)
        }

        if teste {
            let appPhotos = AppPhotos()
            var apps = [App]()

            let app = App()
            app.url = NSURL(string: "https://url2") as URL?

            apps.append(app)
            app.url = NSURL(string: "https://url3") as URL?
            apps.append(app)

            appPhotos.data_consulta = "2"
            appPhotos.apps = apps

            fotos.append(appPhotos)
        }

        for line in fotos {
            for url in line.apps! {
                print(url.url ?? "")
            }
        }
    }
}

class App: NSObject {
    var photo_original:UIImage?
    var url:URL?
}

AppPhotos.testePhotos()

印刷:

https://url1
https://url3
https://url3

will be 

https://url1
https://url2
https://url3

我需要什么,这是:

[ 
    data_consulta : 1,
        app : [ "https://url1" ],
    data_consulta : 2,
        app : [ "https://url2", "https://url3" ]
]

2 个答案:

答案 0 :(得分:3)

会发生什么:

class App {
    var url:URL?
}
var apps = [App]()

let app = App() //app1 created

app.url = URL(string: "https://url2")
apps.append(app)
// apps contains [app1] here

app.url = URL(string: "https://url3") // changes url of app1 ...
apps.append(app) // and adds the same app (app1) again
// apps contains [app1, app1] here

要解决此问题,您必须创建第二个应用:

let app = App() //app1 created

app.url = URL(string: "https://url2")
apps.append(app)
// apps contains [app1] here

let app2 = App() //app2 created
app2.url = URL(string: "https://url3") // changes url of app2
apps.append(app2)
// apps contains [app1, app2] here

更新

或30个应用程序:

let numApps = 30

for i in 1...numApps {
    let app = App()
    app.url = URL(string: "https://url\(i)")
    apps.append(app)
}

答案 1 :(得分:2)

没有人回答为什么程序错误,所以你将来可能会犯同样的错误。 以下是您的代码的工作方式:

        //Declaring a reference to a App istance
        let app = App()
        //changing value of url of that app
        app.url = NSURL(string: "https://url2") as URL?
        //append that REFERENCE to istance
        apps.append(app)

        //Changing the value USING THE SAME REFERENCE 
        app.url = NSURL(string: "https://url3") as URL?

        //Appending the same reference, so both indexes are pointing to an istance with url "https://url3"
        apps.append(app)

解决方案是:

        //Declaring a reference to a App istance
        let app = App()
        //changing value of url of that app
        app.url = NSURL(string: "https://url2") as URL?
        //append that istance
        apps.append(app)
        //Declaring a DIFFERENT reference to another App istance
        let app2 = App()
        //Changing the value of url for that istance
        app2.url = NSURL(string: "https://url3") as URL?
        //Appending the new app
        apps.append(app2)

您还可以将Struct用于App对象。结构将按值复制,因此您不必为引用而烦恼。但是恕我直言,代码的可读性会降低。