Cannot convert value of type '[String]' to expected argument type '([AnyObject]) -> Void'












-2














I have a project code as below, I wish it can get urls from func getUrls(), but Xcode returns error message like title says.



I have search and try some solution to fix it, but all not works. Should I write it with another way, or just fix this error ? Is the declaration for arrUrls was wrong, or somewhere need to make correction?



p.s. If you answer me by a comment, remember to teach me how to make an "Answered mark" for your answer.:) Thank you.



var arrUrls = [String]()

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()


getUrls(url: url, completion: arrUrls) // Error Message: Cannot convert value of type '[String]' to expected argument type '([AnyObject]) -> Void'

...do something with array 'arrUrl'....but can't, because of the bug!

}



func getUrls(url : URL ,completion: @escaping (([AnyObject]) -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? [String : AnyObject] {

if let subjects = jsonResult["subjects"] as? [AnyObject]? {

for subject in subjects! {
if let content = subject["content"] as? [String : AnyObject] {

let s = String(describing: content["url"]!)
arrUrls.append(s)
}
}
}
}
completion(arrUrls as [AnyObject])
}catch {
print("json error: (error)")
}
})
task.resume()
}









share|improve this question




















  • 3




    Remove the line, start writing it again, and let XCode autocomplete help you.
    – Larme
    Nov 20 at 16:54










  • Unrelated but in Swift 3+ the unspecified type is Any, not AnyObject and don't misuse String(describing:)
    – vadian
    Nov 20 at 16:56












  • vadian: It still shows "Cannot convert value of type '[String]' to expected argument type '([Any]) -> Void'", after I replace AnyObject wit Any.......
    – Simon
    Nov 20 at 17:24










  • I would strongly recommend looking into using Codable to avoid JSON parsing.: raywenderlich.com/…
    – PeejWeej
    Nov 20 at 17:30










  • I know, hence I wrote unrelated
    – vadian
    Nov 20 at 17:34
















-2














I have a project code as below, I wish it can get urls from func getUrls(), but Xcode returns error message like title says.



I have search and try some solution to fix it, but all not works. Should I write it with another way, or just fix this error ? Is the declaration for arrUrls was wrong, or somewhere need to make correction?



p.s. If you answer me by a comment, remember to teach me how to make an "Answered mark" for your answer.:) Thank you.



var arrUrls = [String]()

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()


getUrls(url: url, completion: arrUrls) // Error Message: Cannot convert value of type '[String]' to expected argument type '([AnyObject]) -> Void'

...do something with array 'arrUrl'....but can't, because of the bug!

}



func getUrls(url : URL ,completion: @escaping (([AnyObject]) -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? [String : AnyObject] {

if let subjects = jsonResult["subjects"] as? [AnyObject]? {

for subject in subjects! {
if let content = subject["content"] as? [String : AnyObject] {

let s = String(describing: content["url"]!)
arrUrls.append(s)
}
}
}
}
completion(arrUrls as [AnyObject])
}catch {
print("json error: (error)")
}
})
task.resume()
}









share|improve this question




















  • 3




    Remove the line, start writing it again, and let XCode autocomplete help you.
    – Larme
    Nov 20 at 16:54










  • Unrelated but in Swift 3+ the unspecified type is Any, not AnyObject and don't misuse String(describing:)
    – vadian
    Nov 20 at 16:56












  • vadian: It still shows "Cannot convert value of type '[String]' to expected argument type '([Any]) -> Void'", after I replace AnyObject wit Any.......
    – Simon
    Nov 20 at 17:24










  • I would strongly recommend looking into using Codable to avoid JSON parsing.: raywenderlich.com/…
    – PeejWeej
    Nov 20 at 17:30










  • I know, hence I wrote unrelated
    – vadian
    Nov 20 at 17:34














-2












-2








-2







I have a project code as below, I wish it can get urls from func getUrls(), but Xcode returns error message like title says.



I have search and try some solution to fix it, but all not works. Should I write it with another way, or just fix this error ? Is the declaration for arrUrls was wrong, or somewhere need to make correction?



p.s. If you answer me by a comment, remember to teach me how to make an "Answered mark" for your answer.:) Thank you.



var arrUrls = [String]()

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()


getUrls(url: url, completion: arrUrls) // Error Message: Cannot convert value of type '[String]' to expected argument type '([AnyObject]) -> Void'

...do something with array 'arrUrl'....but can't, because of the bug!

}



func getUrls(url : URL ,completion: @escaping (([AnyObject]) -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? [String : AnyObject] {

if let subjects = jsonResult["subjects"] as? [AnyObject]? {

for subject in subjects! {
if let content = subject["content"] as? [String : AnyObject] {

let s = String(describing: content["url"]!)
arrUrls.append(s)
}
}
}
}
completion(arrUrls as [AnyObject])
}catch {
print("json error: (error)")
}
})
task.resume()
}









share|improve this question















I have a project code as below, I wish it can get urls from func getUrls(), but Xcode returns error message like title says.



I have search and try some solution to fix it, but all not works. Should I write it with another way, or just fix this error ? Is the declaration for arrUrls was wrong, or somewhere need to make correction?



p.s. If you answer me by a comment, remember to teach me how to make an "Answered mark" for your answer.:) Thank you.



var arrUrls = [String]()

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()


getUrls(url: url, completion: arrUrls) // Error Message: Cannot convert value of type '[String]' to expected argument type '([AnyObject]) -> Void'

...do something with array 'arrUrl'....but can't, because of the bug!

}



func getUrls(url : URL ,completion: @escaping (([AnyObject]) -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? [String : AnyObject] {

if let subjects = jsonResult["subjects"] as? [AnyObject]? {

for subject in subjects! {
if let content = subject["content"] as? [String : AnyObject] {

let s = String(describing: content["url"]!)
arrUrls.append(s)
}
}
}
}
completion(arrUrls as [AnyObject])
}catch {
print("json error: (error)")
}
})
task.resume()
}






ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 11:09









Rengers

7,72512343




7,72512343










asked Nov 20 at 16:51









Simon

74




74








  • 3




    Remove the line, start writing it again, and let XCode autocomplete help you.
    – Larme
    Nov 20 at 16:54










  • Unrelated but in Swift 3+ the unspecified type is Any, not AnyObject and don't misuse String(describing:)
    – vadian
    Nov 20 at 16:56












  • vadian: It still shows "Cannot convert value of type '[String]' to expected argument type '([Any]) -> Void'", after I replace AnyObject wit Any.......
    – Simon
    Nov 20 at 17:24










  • I would strongly recommend looking into using Codable to avoid JSON parsing.: raywenderlich.com/…
    – PeejWeej
    Nov 20 at 17:30










  • I know, hence I wrote unrelated
    – vadian
    Nov 20 at 17:34














  • 3




    Remove the line, start writing it again, and let XCode autocomplete help you.
    – Larme
    Nov 20 at 16:54










  • Unrelated but in Swift 3+ the unspecified type is Any, not AnyObject and don't misuse String(describing:)
    – vadian
    Nov 20 at 16:56












  • vadian: It still shows "Cannot convert value of type '[String]' to expected argument type '([Any]) -> Void'", after I replace AnyObject wit Any.......
    – Simon
    Nov 20 at 17:24










  • I would strongly recommend looking into using Codable to avoid JSON parsing.: raywenderlich.com/…
    – PeejWeej
    Nov 20 at 17:30










  • I know, hence I wrote unrelated
    – vadian
    Nov 20 at 17:34








3




3




Remove the line, start writing it again, and let XCode autocomplete help you.
– Larme
Nov 20 at 16:54




Remove the line, start writing it again, and let XCode autocomplete help you.
– Larme
Nov 20 at 16:54












Unrelated but in Swift 3+ the unspecified type is Any, not AnyObject and don't misuse String(describing:)
– vadian
Nov 20 at 16:56






Unrelated but in Swift 3+ the unspecified type is Any, not AnyObject and don't misuse String(describing:)
– vadian
Nov 20 at 16:56














vadian: It still shows "Cannot convert value of type '[String]' to expected argument type '([Any]) -> Void'", after I replace AnyObject wit Any.......
– Simon
Nov 20 at 17:24




vadian: It still shows "Cannot convert value of type '[String]' to expected argument type '([Any]) -> Void'", after I replace AnyObject wit Any.......
– Simon
Nov 20 at 17:24












I would strongly recommend looking into using Codable to avoid JSON parsing.: raywenderlich.com/…
– PeejWeej
Nov 20 at 17:30




I would strongly recommend looking into using Codable to avoid JSON parsing.: raywenderlich.com/…
– PeejWeej
Nov 20 at 17:30












I know, hence I wrote unrelated
– vadian
Nov 20 at 17:34




I know, hence I wrote unrelated
– vadian
Nov 20 at 17:34












1 Answer
1






active

oldest

votes


















0














You need to call like this



getUrls(url: url) { (arr) in


}


if you edit the array inside the function then no need to return a completion value



func getUrls(url : URL  ,completion: @escaping (() -> Void)) 


with



getUrls(url: url) {  

// refresh ui here

DispatchQueue.main.async {
if firstItem = self.arrUrls.first , let url = URL(string:firstItem) {
webView.load(URLRequest(url: url))
}
}




change code to this



func getUrls(url : URL  ,completion: @escaping (() -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in

guard let data = data else { return }

do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String : Any] {

if let subjects = jsonResult["subjects"] as? [[String:Any]] {

for subject in subjects {

if let content = subject["content"] as? [String : Any] {

if let s = content["url"] as? String
{
self.arrUrls.append(s)
}
}
}

completion()

}
}

}catch {

completion()
}
})
task.resume()
}





share|improve this answer























  • Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
    – Simon
    Nov 20 at 17:19












  • yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
    – Sh_Khan
    Nov 20 at 17:21












  • This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
    – Simon
    Nov 20 at 17:48










  • After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
    – Simon
    Nov 20 at 17:58












  • @Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
    – Sh_Khan
    Nov 20 at 19:07











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397792%2fcannot-convert-value-of-type-string-to-expected-argument-type-anyobject%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to call like this



getUrls(url: url) { (arr) in


}


if you edit the array inside the function then no need to return a completion value



func getUrls(url : URL  ,completion: @escaping (() -> Void)) 


with



getUrls(url: url) {  

// refresh ui here

DispatchQueue.main.async {
if firstItem = self.arrUrls.first , let url = URL(string:firstItem) {
webView.load(URLRequest(url: url))
}
}




change code to this



func getUrls(url : URL  ,completion: @escaping (() -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in

guard let data = data else { return }

do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String : Any] {

if let subjects = jsonResult["subjects"] as? [[String:Any]] {

for subject in subjects {

if let content = subject["content"] as? [String : Any] {

if let s = content["url"] as? String
{
self.arrUrls.append(s)
}
}
}

completion()

}
}

}catch {

completion()
}
})
task.resume()
}





share|improve this answer























  • Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
    – Simon
    Nov 20 at 17:19












  • yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
    – Sh_Khan
    Nov 20 at 17:21












  • This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
    – Simon
    Nov 20 at 17:48










  • After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
    – Simon
    Nov 20 at 17:58












  • @Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
    – Sh_Khan
    Nov 20 at 19:07
















0














You need to call like this



getUrls(url: url) { (arr) in


}


if you edit the array inside the function then no need to return a completion value



func getUrls(url : URL  ,completion: @escaping (() -> Void)) 


with



getUrls(url: url) {  

// refresh ui here

DispatchQueue.main.async {
if firstItem = self.arrUrls.first , let url = URL(string:firstItem) {
webView.load(URLRequest(url: url))
}
}




change code to this



func getUrls(url : URL  ,completion: @escaping (() -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in

guard let data = data else { return }

do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String : Any] {

if let subjects = jsonResult["subjects"] as? [[String:Any]] {

for subject in subjects {

if let content = subject["content"] as? [String : Any] {

if let s = content["url"] as? String
{
self.arrUrls.append(s)
}
}
}

completion()

}
}

}catch {

completion()
}
})
task.resume()
}





share|improve this answer























  • Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
    – Simon
    Nov 20 at 17:19












  • yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
    – Sh_Khan
    Nov 20 at 17:21












  • This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
    – Simon
    Nov 20 at 17:48










  • After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
    – Simon
    Nov 20 at 17:58












  • @Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
    – Sh_Khan
    Nov 20 at 19:07














0












0








0






You need to call like this



getUrls(url: url) { (arr) in


}


if you edit the array inside the function then no need to return a completion value



func getUrls(url : URL  ,completion: @escaping (() -> Void)) 


with



getUrls(url: url) {  

// refresh ui here

DispatchQueue.main.async {
if firstItem = self.arrUrls.first , let url = URL(string:firstItem) {
webView.load(URLRequest(url: url))
}
}




change code to this



func getUrls(url : URL  ,completion: @escaping (() -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in

guard let data = data else { return }

do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String : Any] {

if let subjects = jsonResult["subjects"] as? [[String:Any]] {

for subject in subjects {

if let content = subject["content"] as? [String : Any] {

if let s = content["url"] as? String
{
self.arrUrls.append(s)
}
}
}

completion()

}
}

}catch {

completion()
}
})
task.resume()
}





share|improve this answer














You need to call like this



getUrls(url: url) { (arr) in


}


if you edit the array inside the function then no need to return a completion value



func getUrls(url : URL  ,completion: @escaping (() -> Void)) 


with



getUrls(url: url) {  

// refresh ui here

DispatchQueue.main.async {
if firstItem = self.arrUrls.first , let url = URL(string:firstItem) {
webView.load(URLRequest(url: url))
}
}




change code to this



func getUrls(url : URL  ,completion: @escaping (() -> Void)) {

let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in

guard let data = data else { return }

do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String : Any] {

if let subjects = jsonResult["subjects"] as? [[String:Any]] {

for subject in subjects {

if let content = subject["content"] as? [String : Any] {

if let s = content["url"] as? String
{
self.arrUrls.append(s)
}
}
}

completion()

}
}

}catch {

completion()
}
})
task.resume()
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 19:02

























answered Nov 20 at 16:53









Sh_Khan

37.8k51125




37.8k51125












  • Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
    – Simon
    Nov 20 at 17:19












  • yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
    – Sh_Khan
    Nov 20 at 17:21












  • This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
    – Simon
    Nov 20 at 17:48










  • After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
    – Simon
    Nov 20 at 17:58












  • @Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
    – Sh_Khan
    Nov 20 at 19:07


















  • Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
    – Simon
    Nov 20 at 17:19












  • yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
    – Sh_Khan
    Nov 20 at 17:21












  • This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
    – Simon
    Nov 20 at 17:48










  • After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
    – Simon
    Nov 20 at 17:58












  • @Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
    – Sh_Khan
    Nov 20 at 19:07
















Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
– Simon
Nov 20 at 17:19






Thank you. But webview should change setting in main thread only...(Xcode says), so I only can wait URLSession.shared.dataTask finish it's work, and save all url to array, and then change web view setting.....
– Simon
Nov 20 at 17:19














yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
– Sh_Khan
Nov 20 at 17:21






yes when you call that function embed any ui content inside DispatchQueue.main.async as completion is inside a background thread
– Sh_Khan
Nov 20 at 17:21














This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
– Simon
Nov 20 at 17:48




This seems to be work, but.. where I set web view url? Because webview only can change setting in main thread..., this is a problem.
– Simon
Nov 20 at 17:48












After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
– Simon
Nov 20 at 17:58






After I change the code, the error message change to "Cannot convert value of type '[String]' to expected argument type '() -> Void'"........ It's seems always the type not match....
– Simon
Nov 20 at 17:58














@Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
– Sh_Khan
Nov 20 at 19:07




@Simon you have an array of urls the webview can load 1 at a time , i updated answer with loading first url see edit..............
– Sh_Khan
Nov 20 at 19:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397792%2fcannot-convert-value-of-type-string-to-expected-argument-type-anyobject%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)