How to pass Swift array as parameter to thread?


chapka

I'm trying to spawn a new thread to perform some background processing based on a String that has been decomposed into an array of characters. Here is what my code looks like:

   var testString : String = NSString(data:data!, encoding:NSUTF8StringEncoding)
   var testStringArray : Array<Character> = []

   for character in testString
   {
       if(!(self.isCharacterStrippable(character)))
       {
           testStringArray.append(character)
       }
   }

   NSThread.detachNewThreadSelector("fillKeysFromArray:", toTarget: self, withObject: testStringArray)

I get a compiler error telling me "Array does not conform to protocol AnyObject".

Without writing an object wrapper for the array, or making it an instance variable (both seem like overkill), is there any way I can make this array pass to a new thread?

Mike S

Using Grand Central Dispatch will be much easier in the long run . You can run a function on a background thread using:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
    [weak self] in // This is so that we don't create a reference cycle
    self?.fillKeysFromArray(testStringArray);
    return
}

You can read about Grand Central Dispatch and all the features it offers in terms of threads and concurrency in Apple's Concurrency Programming Guide .

Related


How to pass Swift array as parameter to thread?

chapka I'm trying to spawn a new thread to perform some background processing based on a String that has been decomposed into an array of characters. Here is what my code looks like: var testString : String = NSString(data:data!, encoding:NSUTF8StringEncodi

Pass array as parameter in sqlite Swift

Chongzl I am implementing SQLite in my project by adding #import <sqlite3.h>my header.hfiles libsqlite3.dylib. How can I pass an array as a parameter to the query, here are my thoughts: var arrayId = [1,2] // array with interested Id var query:NSString = "Sel

Pass array as parameter in sqlite Swift

Chongzl I am implementing SQLite in my project by adding #import <sqlite3.h>my header.hfiles libsqlite3.dylib. How can I pass an array as a parameter to the query, here are my thoughts: var arrayId = [1,2] // array with interested Id var query:NSString = "Sel

How to pass protocol as parameter in Swift

Jean LeBrunmont In Objective-C, I know how to pass the protocolas parameter: - (void)MyMethod:(Protocol *)myparameter But in Swift there are no more Protocoltypes. How can I pass a protocol as a parameter without knowing which? Joachim In one of your comments

How to pass a class as a parameter in Swift?

Doug Smith I want to write a function with the following signature: func viewHasSuperviewOfClass(view: UIView, superclass: AnyObject.Type) -> Bool { return view.superview is superclass } But it won't compile. What am I doing wrong? How can I pass the supe

How to pass an array to a thread in Java?

User 4653841 I want to pass array to thread and display in it. But I don't know how to pass the array to the thread? I have tried a function like passing an array but it doesn't work. I have two threads in which I will print the array. this is my topic 1 publi

How to pass an array to a thread in Java?

User 4653841 I want to pass array to thread and display in it. But I don't know how to pass the array to the thread? I have tried a function like passing an array but it doesn't work. I have two threads in which I will print the array. this is my topic 1 publi

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass an array parameter to a function

Peter XX I have implemented a simple merge sort function in PowerShell as follows function Merge-Sort { param($a) if ($a.Length -gt 1) { $m = [Math]::floor($a.Length / 2) [Int[]]$l = $a[0..($m-1)] [Int[]]$r = $a[$m..($a.Length)] Merge-So

How to pass array as parameter in Java?

username I tried using an array as a parameter, but when trying to pass an array as a parameter, I get the following error: Required: int, int[] found: int, int Cause: Actual argument int cannot be converted to int[] by method call conversion 2 Source code: pu

How to pass an array as a function parameter?

Ahsanul Haque Struggled with passing arrays as parameters for a while, but it didn't work anyway. I have tried as follows: #! /bin/bash function copyFiles{ arr="$1" for i in "${arr[@]}"; do echo "$i" done } array=("one" "two" "th

How to pass an array as a function parameter?

Ahsanul Haque Struggled with passing arrays as parameters for a while, but it didn't work anyway. I have tried as follows: #! /bin/bash function copyFiles{ arr="$1" for i in "${arr[@]}"; do echo "$i" done } array=("one" "two" "th

How to pass an array parameter to a function

Peter XX I have implemented a simple merge sort function in PowerShell as follows function Merge-Sort { param($a) if ($a.Length -gt 1) { $m = [Math]::floor($a.Length / 2) [Int[]]$l = $a[0..($m-1)] [Int[]]$r = $a[$m..($a.Length)] Merge-So

How to pass an array as a function parameter?

Ahsanul Haque Struggled with passing arrays as parameters for a while, but it didn't work anyway. I have tried as follows: #! /bin/bash function copyFiles{ arr="$1" for i in "${arr[@]}"; do echo "$i" done } array=("one" "two" "th

Android: Pass parameter to thread

Dysfunction I'm trying to learn Android programming and so far so good. Now I'm trying to learn a bit about threading, but I can't get my code to work. i have seen http://developer.android.com/guide/faq/commontasks.html#threading http://www.vogella.com/tutoria

Pass parameter to Java thread

Shunta I already know: How to pass parameters to a Java thread? But I don't know how to use it. So I wrote simple sample code to save your precious time: class ThreadParam implements Runnable { static int c; public ThreadParam(int a, int b){ int c = a+b;

Pass parameter to Java thread

Shunta I already know: How to pass parameters to a Java thread? But I don't know how to use it. So I wrote simple sample code to save your precious time: class ThreadParam implements Runnable { static int c; public ThreadParam(int a, int b){ int c = a+b;

Pass parameter to thread listener

username I have server-client application. Servers and clients can send and receive files using TCP and streams. The file transfer is handled in a listener which has its own thread in my class like this: static void Main(string[] args) { Thread thread = ne