Because 2D arrays in swift can have subarrays with different lengths. There is no "matrix" type.
let arr = [
[1,2,3,4,5],
[1,2,3],
[2,3,4,5],
]
So the concept of "rows" and "columns" does not exist. There's only count.
If you want to count all the elements in the subarrays, (in the above case, 12), you can flat map it and then count:
arr.flatMap { $0 }.count
If you are sure that your array is a matrix, you can do this:
let rows = arr.count
let columns = arr[0].count // 0 is an arbitrary value
Answer from Sweeper on Stack OverflowDetermine Size of Multidimensional Array in Swift - Stack Overflow
Fixed size array hacks - Using Swift - Swift Forums
Is it possible to limit the size of an array if the user isn't a subscriber
Limit array size in swift - Stack Overflow
Because 2D arrays in swift can have subarrays with different lengths. There is no "matrix" type.
let arr = [
[1,2,3,4,5],
[1,2,3],
[2,3,4,5],
]
So the concept of "rows" and "columns" does not exist. There's only count.
If you want to count all the elements in the subarrays, (in the above case, 12), you can flat map it and then count:
arr.flatMap { $0 }.count
If you are sure that your array is a matrix, you can do this:
let rows = arr.count
let columns = arr[0].count // 0 is an arbitrary value
You must ask the size of a specific row of your array to get column sizes :
print("\(a.count) \(a[0].count)")
I build an app what stores data in arrays in CoreData. Now I was wondering if it's possible to limit the capacity of the array based on the subscription level?
Nope, Swift doesn't come with this kind of array - which is similar to a View from a database - allowing you to peek the first N elements. Though for this both the view and it target should be reference types, which is not the case in Swift for arrays.
But enough blabbing around, you could quickly write a wrapper over an Array for fulfilling your needs:
/// an array-like struct that has a fixed maximum capacity
/// any element over the maximum allowed size gets discarded
struct LimitedArray<T> {
private(set) var storage: [T] = []
public let maxSize: Int
/// creates an empty array
public init(maxSize: Int) {
self.maxSize = maxSize
}
/// takes the max N elements from the given collection
public init<S: Sequence>(from other: S, maxSize: Int) where S.Element == T {
self.maxSize = maxSize
storage = Array(other.prefix(maxSize))
}
/// adds a new item to the array, does nothing if the array has reached its maximum capacity
/// returns a bool indicated the operation success
@discardableResult public mutating func append(_ item: T) -> Bool {
if storage.count < maxSize {
storage.append(item)
return true
} else {
return false
}
}
/// inserts an item at the specified position. if this would result in
/// the array exceeding its maxSize, the extra element are dropped
public mutating func insert(_ item: T, at index: Int) {
storage.insert(item, at: index)
if storage.count > maxSize {
storage.remove(at: maxSize)
}
}
// add here other methods you might need
}
// let's benefit all the awesome operations like map, flatMap, reduce, filter, etc
extension LimitedArray: MutableCollection {
public var startIndex: Int { return storage.startIndex }
public var endIndex: Int { return storage.endIndex }
public subscript(_ index: Int) -> T {
get { return storage[index] }
set { storage[index] = newValue }
}
public func index(after i: Int) -> Int {
return storage.index(after: i)
}
}
Since the struct conforms to Collection, you can easily pass it to code that knows only to work with arrays by transforming its contents into an array: Array(myLimitedArray).
simply count the elements of array before appending new element.
var array :[Int] = [1,2,3,4,5,6,7,8,9,10]
func insertIntoArray(_ value: Int, array: [Int]) -> [Int] {
var arr = array
if arr.count == 10 {
arr.removeLast()
}
arr.append(value)
return arr
}
array = insertIntoArray(11, array: array)
Fixed-length arrays are not yet supported. What does that actually mean? Not that you can't create an array of n many things โ obviously you can just do let a = [ 1, 2, 3 ] to get an array of three Ints. It means simply that array size is not something that you can declare as type information.
If you want an array of nils, you'll first need an array of an optional type โ [SKSpriteNode?], not [SKSpriteNode] โ if you declare a variable of non-optional type, whether it's an array or a single value, it cannot be nil. (Also note that [SKSpriteNode?] is different from [SKSpriteNode]?... you want an array of optionals, not an optional array.)
Swift is very explicit by design about requiring that variables be initialized, because assumptions about the content of uninitialized references are one of the ways that programs in C (and some other languages) can become buggy. So, you need to explicitly ask for an [SKSpriteNode?] array that contains 64 nils:
var sprites = SKSpriteNode?
The best you are going to be able to do for now is create an array with an initial count repeating nil:
var sprites = SKSpriteNode?
You can then fill in whatever values you want.
In Swift 3.0 :
var sprites = SKSpriteNode?
Try this.
var computeArray: Array<Float> = Array(repeating: 0, count: 1000)
or with nils
var computeArray: Array<Float?> = Array(repeating: nil, count: 1000)
Swift is a type-safe language. This essentially means that you can't store a value of some other type (here nil) in a variable/ constant of a particular type (here Float).
So, if you want to store nil values in an array, declare its element type as optional (here Float?).
var computeArray = Float?
or
var computeArray = Array<Float?>(repeating: nil, count:1000)