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:
Copyvar sprites = SKSpriteNode?
Answer from rickster on Stack OverflowVideos
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:
Copyvar sprites = SKSpriteNode?
The best you are going to be able to do for now is create an array with an initial count repeating nil:
Copyvar sprites = SKSpriteNode?
You can then fill in whatever values you want.
In Swift 3.0 :
Copyvar sprites = SKSpriteNode?
You can get the number of elements in an array simply with
let count = testArray.count
and the total number of bytes of its elements with
var arrayLength = testArray.count * sizeof(Int32)
// Swift 3:
var arrayLength = testArray.count * MemoryLayout<Int32>.size
sizeof is used with types and sizeofValue with values, so both
var arrayLength = sizeof([Int32])
var arrayLength = sizeofValue(testArray)
would compile. But that gives you the size of the struct Array, not the size
of the element storage.
In Xcode 8 with Swift 3 beta 6 there is no function sizeof (). But if you want, you can define one for your needs. The good news are, that this new sizeof function works as expected with your array.
let bb: UInt8 = 1
let dd: Double = 1.23456
func sizeof <T> (_ : T.Type) -> Int
{
return (MemoryLayout<T>.size)
}
func sizeof <T> (_ : T) -> Int
{
return (MemoryLayout<T>.size)
}
func sizeof <T> (_ value : [T]) -> Int
{
return (MemoryLayout<T>.size * value.count)
}
sizeof(UInt8.self) // 1
sizeof(Bool.self) // 1
sizeof(Double.self) // 8
sizeof(dd) // 8
sizeof(bb) // 1
var testArray: [Int32] = [2000,400,5000,400]
var arrayLength = sizeof(testArray) // 16
You need all versions of the sizeof function, to get the size of a variable and to get the correct size of a data-type and of an array.
If you only define the second function, then sizeof(UInt8.self) and sizeof(Bool.self) will result in "8". If you only define the first two functions, then sizeof(testArray) will result in "8".
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)
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)
You need to specify the range. If you want to include nameArrayLength:
for index in 1...nameArrayLength {
}
If you want to stop 1 before nameArrayLength:
for index in 1..<nameArrayLength {
}
for i in 0..< names.count {
//YOUR LOGIC....
}
for name in 0..< names.count {
//YOUR LOGIC....
print(name)
}
for (index, name) in names.enumerated()
{
//YOUR LOGIC....
print(name)
print(index)//0, 1, 2, 3 ...
}