Questions tagged [swift-array]

An array in swift programming stores multiple values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.

Swift arrays are specific about the kinds of values they can store. They differ from Objective-C’s NSArray and NSMutableArray classes, which can store any kind of object and do not provide any information about the nature of the objects they return.

In Swift, the type of values that a particular array can store is always made clear, either through an explicit type annotation, or through type inference, and does not have to be a class type. If you create an array of Int values, for example, you can’t insert any value other than Int values into that array. Swift arrays are type safe, and are always clear about what they may contain.

link: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-XID_172

22 questions
19
votes
1 answer

Monitor changes to an array object

I have no idea if this is possible at all, but it would save me from a lot of stress and bad code. Is it possible to monitor whenever an array gets updated? For example, method A changes the array a=[1,2,3] to a=[1,2,3,4], is it possible to have a…
Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92
7
votes
3 answers

Efficiently copying Swift Array to memory buffer for iOS Metal

I am writing an iOS application using Apple's new Metal framework. I have an array of Matrix4 objects (see Ray Wenderlich's tutorial) that I need to pass in to a shader via the MTLDevice.newBufferWithLength() method. The Matrix4 object is…
Dead Pixel
  • 433
  • 4
  • 9
7
votes
4 answers

How to convert NSSet to [String] array?

I have an NSSet of Strings, and I want to convert it into [String]. How do I do that?
noobprogrammer
  • 345
  • 1
  • 6
  • 20
3
votes
1 answer

Cannot subscript a value of type '[AnyObject]?' with an index of type 'UInt32'

The goal of the code below is this - get all objects from the parse.com class called OnlineUsers and then find a RANDOM number between 0 and results.count and then use that object for something. Issue is that when I try to get the object at position…
user1406716
  • 9,565
  • 22
  • 96
  • 151
3
votes
2 answers

How do you assign a slice of numbers to an array in swift

x is an object that holds an array called point. x implements the subscript operator so you can do things, like x[i] to get the array's ith element (of type T, which is usually an Int or Double). This is what I want to do: x[0...2] = [0...2] But…
user678392
  • 1,981
  • 3
  • 28
  • 50
2
votes
2 answers

Swift Array Declaration Difference

What is the difference between below declaration of array syntax in swift? var arr:[Int] var arr=Array() and which one is better? How and Why?
Amit Jagesha シ
  • 1,092
  • 12
  • 21
2
votes
1 answer

Create an Array of multiple Arrays in Swift

i have an object with some proprieties, arrays of classes or structs I want to put those proprieties in an array so i can access them easily in TableDataSource ( because of section, indexpath etc.. ) I've tried writing this initialization var…
2
votes
2 answers

How not to append item from UITextField to array if empty or blank spaces - Swift

This is my very first question on Stack Overflow so I hope I'm following the rules correctly. I'm making a To-Do list app in Swift and I'm trying to prevent blank or empty spaces from being appended to the table. My UITextField: @IBOutlet weak var…
JohnJLilley
  • 173
  • 1
  • 2
  • 10
1
vote
2 answers

How to save an array of strings using NSUserDefaults in swift

I am trying to save an string array using NSUserDefaults with swift. I have searched around and believe that i have to use [NSString] not [String] however the app still crashes with error fatal error: unexpectedly found nil while unwrapping an…
Matt
  • 688
  • 5
  • 15
1
vote
1 answer

Is there a way to initialize an array in Swift to have a repeated value of some Type T

In my init function, I have: self.x = [T](count: dimensions, repeatedValue: 0) This does not work. How do I get this to work. I want x to be an array with type T that is initialized to 0. (T is intuitively like Int but could be some other number…
user678392
  • 1,981
  • 3
  • 28
  • 50
1
vote
2 answers

Remove elements from an array that contains particular string in swift

I have an array. var array_var: [String] =…
user4423327
1
vote
4 answers

Swift: dictionaries inside array

Data: [ { firstName: "Foo", lastName: "Bar" }, { firstName: "John", lastName: "Doe" } ] How can I have this kind of structure using swift array and dictionary? This data shows dictionaries inside an array, right? So I suggest: var…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
0
votes
0 answers

Saving and retrieving images with correct order

I'm trying save some images to an array, and their name to another array as string with below code; self.countryNamesArray.append(object["CountryName"] as! String) let image = UIImage(data:imageData!) self.countryImages.append(image!) But it…
mehmeet43
  • 183
  • 15
0
votes
1 answer

Swift - Search using UISearchController and NSPredicate with an array of structs

Currently I'm trying to set up a search bar for my tableView. I want to filter the user's input with NSPredicate to display filtered data from an array of struct objects. But when I try to call .filteredArrayUsingPredicate() on such an array, I get…
0
votes
2 answers

Display UIImage within an array with custom class in Swift

I have a custom class Product defined as class Product: NSObject { var name: String var priceLabel: String var productImage: UIImage init(name: String, priceLabel: String, productImage: UIImage) { self.name = name self.priceLabel =…
1
2