class Facts {
var networkOperaton = NetworkOperation(url: "http://fact.tayfunturanligil.com")
var factsArray : [String] = []
init () {
self.networkOperaton.downloadJSONFromURL({
(a:[String]) -> [String] in
println(a)
self.factsArray = a
return a
})
}
}
When I want to create an instance of Facts in my ViewController using var facts = Facts()
, factsArray
stays as an empty array. But it should be getting an array from downloadJSONFromURL
function. Why this is happening? Even my println(a)
is not called.
当我想使用var Facts = Facts()在我的ViewController中创建一个事实实例时,factsArray保持为一个空数组。但是它应该是从downloadJSONFromURL函数中获得一个数组。这是为什么呢?甚至我的println(a)也不被调用。
I tested downloadJSONFromURL
function in a playground and it works. Also, networkOperation
uses NSURLSession
, could this be an issue?
我在一个操场上测试了downloadJSONFromURL函数,它运行良好。另外,networkOperation使用NSURLSession,这是一个问题吗?
2 个解决方案
#1
1
The init()
method is actually called. Try out this code:
init()方法实际上被调用。试试这段代码:
class Facts {
var factsArray : [String] = []
init () {
println("Init called")
factsArray = ["Hello", "World", "Bye", "World"]
}
}
ViewController.swift:
ViewController.swift:
override func viewDidLoad() {
super.viewDidLoad()
var facts = Facts()
println(facts.factsArray)
}
The problem with your code would be that network operation would be running in asynchronous mode. Instead of making network request in init method you can define another method in function which can then return status of request in completion block.
您的代码的问题是,网络操作将以异步模式运行。与在init方法中发出网络请求不同,您可以在函数中定义另一个方法,该方法可以在完成块中返回请求的状态。
func updateArrayFromNetwork(completion: (Bool) -> ()) {
var networkOperaton = NetworkOperation(url: "http://fact.tayfunturanligil.com")
self.networkOperaton.downloadJSONFromURL({
(a:[String]) -> [String] in
println(a)
self.factsArray = a
completion(true)
})
}
ViewController.swift
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
var facts = Facts()
println(facts.factsArray)
facts.updateArrayFromNetwork { (completed: Bool) -> () in
if completed {
//Do something
}
}
}
#2
0
init()
is called but downloadJSONFromURL()
is an asynchronous method.
That means init()
returns immediately the Facts
instance after calling downloadJSONFromURL()
without waiting for completion.
init()被调用,但是downloadJSONFromURL()是一个异步方法。这意味着init()在调用downloadJSONFromURL()之后立即返回事实实例,而无需等待完成。
#1
1
The init()
method is actually called. Try out this code:
init()方法实际上被调用。试试这段代码:
class Facts {
var factsArray : [String] = []
init () {
println("Init called")
factsArray = ["Hello", "World", "Bye", "World"]
}
}
ViewController.swift:
ViewController.swift:
override func viewDidLoad() {
super.viewDidLoad()
var facts = Facts()
println(facts.factsArray)
}
The problem with your code would be that network operation would be running in asynchronous mode. Instead of making network request in init method you can define another method in function which can then return status of request in completion block.
您的代码的问题是,网络操作将以异步模式运行。与在init方法中发出网络请求不同,您可以在函数中定义另一个方法,该方法可以在完成块中返回请求的状态。
func updateArrayFromNetwork(completion: (Bool) -> ()) {
var networkOperaton = NetworkOperation(url: "http://fact.tayfunturanligil.com")
self.networkOperaton.downloadJSONFromURL({
(a:[String]) -> [String] in
println(a)
self.factsArray = a
completion(true)
})
}
ViewController.swift
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
var facts = Facts()
println(facts.factsArray)
facts.updateArrayFromNetwork { (completed: Bool) -> () in
if completed {
//Do something
}
}
}
#2
0
init()
is called but downloadJSONFromURL()
is an asynchronous method.
That means init()
returns immediately the Facts
instance after calling downloadJSONFromURL()
without waiting for completion.
init()被调用,但是downloadJSONFromURL()是一个异步方法。这意味着init()在调用downloadJSONFromURL()之后立即返回事实实例,而无需等待完成。