Cannot convert value of type '[String]' to expected argument type '([AnyObject]) -> Void'
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
|
show 4 more comments
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
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 isAny, notAnyObjectand don't misuseString(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
|
show 4 more comments
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
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
ios swift
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 isAny, notAnyObjectand don't misuseString(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
|
show 4 more comments
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 isAny, notAnyObjectand don't misuseString(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
|
show 4 more comments
1 Answer
1
active
oldest
votes
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()
}
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
|
show 1 more comment
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
}
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
|
show 1 more comment
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()
}
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
|
show 1 more comment
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()
}
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()
}
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
|
show 1 more comment
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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, notAnyObjectand don't misuseString(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