No Class/Struct Autocomplete in Swift - swift

I currently have two structs and a class in my sample code. One struct is for Tiger, one is for Balloon and on is for Lion. Each are below:
struct Tiger {
var name = ""
var age = 0
var breed = ""
var image = UIImage(named: "")
func chuff() {
println("\(name): Chuff Chuff")
}
func chuffNumberOfTimes(numberOfTimes:Int) {
for (var i = 0; i < numberOfTimes; i++) {
self.chuff()
}
}
func ageInTigerYearsFromAge(regularAge:Int) -> Int {
return regularAge * 3
}
func randomFact() -> String {
let randomFactNumber = Int(arc4random_uniform(UInt32(3)))
var randomFact:String
switch randomFactNumber {
case 0:
randomFact = "Tigers are 10 feet tall."
case 1:
randomFact = "Tigers are amazing."
case 2:
randomFact = "Tigers have 10 feet."
default:
randomFact = ""
}
return randomFact
}
}
struct Balloon {
var number = 0
var image = UIImage(named: "")
}
class Lion {
var name = ""
var age = 0
var isAlphaMale = false
var image = UIImage(named: "")
var subSpecies = ""
}
Each one is in its own file named identically to the struct/class & ".swift". However, the only one that autocompletes itself while typing in ViewController.swift is the Tiger struct. For instance, if I were to set myTiger = Tiger( it would suggest name: String, age: Int etc. How can I get the other two to do the same? I really like defining the variables inline instead of having 5 lines of code to define all of the variables.
Is anyone familiar with how that works or why the Tiger struct would do it while the Balloon and Lion structs don't?

Classes don't get the initializers for free as the Struct does, if you want to have an initializer for a class you have to create it:
class Lion {
var name = ""
var age = 0
var isAlphaMale = false
var image = UIImage(named: "")
var subSpecies = ""
init(name: String, age:Int, isAlphaMale: Boolean, image: UIImage, subSpecies: String){
self.name = name
self.age = age
self.isAlphaMale = isAlphaMale
self.image = image
self.subSpecies = subSpecies
}
}
The Balloon should work automatically, probably a clean and build in your code so xcode can be updated will fix the problem.
From Apple Documentation:
Memberwise Initializers for Structure Types
All structures have an automatically-generated memberwise initializer,
which you can use to initialize the member properties of new structure
instances. Initial values for the properties of the new instance can
be passed to the memberwise initializer by name:
copied from a previus part of the document to add context
struct Resolution {
var width = 0
var height = 0
}
let vga = Resolution(width: 640, height: 480)
Unlike structures, class
instances do not receive a default memberwise initializer.
With the class above you will have the initializer as in the screenshot below:
As you can see in the screenshot Balloon works perfectly fine as well
No need to enter once to start work, xcode is not perfect and sometimes it needs a clean and a new build to make it be in sync with your classes and structs.

Related

Why is this showing 'Expected 'func' keyword in instance method declaration'

I'm new to coding and using SwiftUI on Xcode, and I don't see what's wrong with this code:
class NormalSpace {
var name = ""
var value = 0
var rent = 0
var owned = false
}
var newRoad = NormalSpace()
newRoad.name = "New Road"
newRoad.value = 600
newRoad.rent = 25
newRoad.owned = false
the error 'Expected 'func' keyword in instance method declaration' only shows on the newRoad.name line. The same line also has the error: Invalid redeclaration of 'newRoad'.
What have I done wrong?
In an normal project, this is not valid:
class NormalSpace {
var name = ""
var value = 0
var rent = 0
var owned = false
}
var newRoad = NormalSpace()
newRoad.name = "New Road"
newRoad.value = 600
newRoad.rent = 25
newRoad.owned = false
You can do that in a playground (where it just runs this code directly), but in an app, code (such as the setting of the properties) belongs in a function or initializer.
That initialization code needs to be placed within some context. Let us imagine that it is inside a struct. But this still is not valid:
class NormalSpace {
var name = ""
var value = 0
var rent = 0
var owned = false
}
struct Foo {
var newRoad = NormalSpace()
newRoad.name = "New Road"
newRoad.value = 600
newRoad.rent = 25
newRoad.owned = false
}
The property, newRoad is fine, but the values are not. You need to wrap it inside a func (hence the error) or an init. E.g., this initializes newRoad during the init of the struct:
struct Foo {
let newRoad: NormalSpace
init() {
newRoad = NormalSpace()
newRoad.name = "New Road"
newRoad.value = 600
newRoad.rent = 25
newRoad.owned = false
}
}
Or you might initialize it in a func:
struct Foo {
var newRoad: NormalSpace?
mutating func bar() {
let road = NormalSpace()
road.name = "New Road"
road.value = 600
road.rent = 25
road.owned = false
newRoad = road
}
}
Or, alternatively, you can initialize this property with a closure (note the extra () at the end):
struct Foo {
let newRoad: NormalSpace = {
let road = NormalSpace()
road.name = "New Road"
road.value = 600
road.rent = 25
road.owned = false
return road
}()
}
But the code where you initialize the properties must be placed within some context, so that the compiler knows when those lines of code should be run.
Note, we would generally give NormalSpace a “memberwise initializer”, e.g.:
class NormalSpace {
let name: String
let value: Int
let rent: Int
let owned: Bool
init(name: String, value: Int, rent: Int, owned: Bool) {
self.name = name
self.value = value
self.rent = rent
self.owned = owned
}
}
Or, if a struct (and we would generally prefer to make our model objects struct value-types rather than class reference-types), this memberwise initializer would be created for you:
struct NormalSpace {
let name: String
let value: Int
let rent: Int
let owned: Bool
}
Either way, you can then provide all the desired values during initialization, e.g.:
struct Foo {
let newRoad = NormalSpace(name: "New Road", value: 600, rent: 25, owned: false)
}
Note, that I've removed the “default” values because those really are not appropriate. If you wanted to say that they do not need to be provided, then you would make them “optionals”. But there is generally a big difference between, say, a rent of zero (i.e. it is my grandmother’s house and she's not charging me) and that no rent has been specified. In Swift, we generally avoid using “sentinel” values like "" or 0 for “no value provided”.
Also, now that we have a memberwise initializer, I have also made the properties immutable (let rather than var). If you need to make them mutable (e.g. to let someone change the rent later), fine, revert back to var. But only make properties mutable if you really need to change them later on.

Cannot convert value of type '<Int>' to expected element type <Any>

I'm trying to learn swift, but I have a problem where using <Object> in Java would fix my problem I think, and the Apple doc says I should use <Any> but I keep getting errors.
I'm trying to build a memorize card game, I have the following models:
Theme.swift <- In charge of modeling different kind of themes for the cards, the idea is that the cards could have numbers, images etc, thats why it has a generic type after the name
import Foundation
import UIKit
struct Theme<Type> {
internal init(name: String, emojis: [Type], numberOfPairs: Int, cardsColor: UIColor) {
self.name = name
self.emojis = emojis
if(numberOfPairs > emojis.count || numberOfPairs < emojis.count) {
fatalError("Index out of bounds")
}
self.numberOfPairs = numberOfPairs
self.cardsColor = cardsColor
}
var name: String
var emojis: [Type]
var numberOfPairs: Int
var cardsColor: UIColor
}
I also have a Game model in charge of the game logic and cards model, I still have to implement a lot of stuff, but here's the code
import Foundation
struct Game {
var themes: [Theme<Any>]
var cards: [Card<Any>]
var score = 0
var isGameOver = false
var choosenTheme: Theme<Any>
init(themes: [Theme<Any>]) {
self.themes = themes
self.choosenTheme = self.themes.randomElement()!
cards = []
for index in 0..\<choosenTheme.numberOfPairs {
cards.append(Card(id: index*2, content: choosenTheme.emojis[index]))
cards.append(Card(id: index*2+1, content: choosenTheme.emojis[index]))
}
}
mutating func endGame() {
isGameOver = true
}
mutating func penalizePoints() {
score -= 1
}
mutating func awardPoints () {
score += 2
}
struct Card<T>: Identifiable {
var id: Int
var isFaceUP: Bool = false
var content: T
var isMatchedUP: Bool = false
var isPreviouslySeen = false
}
}
As you can notice I've used the Any type for creating an array of Cards and themes, cuz they could have strings, numbers or images
In my ViewModel I have the following code, where I'm trying to fill the Array of themes with two themes, one of string type of content, and the other one of Int:
import Foundation
import SwiftUI
class GameViewModel {
static let halloweenTheme = Theme<Int>(name: "WeirdNumbers", emojis: [1, 2, 4, 9, 20, 30], numberOfPairs: 6, cardsColor: .darkGray)
static let emojisTheme = Theme<String>(name: "Faces", emojis: ["🥰", "😄", "😜", "🥳", "🤓", "😎", "😋", "🤩"], numberOfPairs: 5, cardsColor: .blue)
var gameController: Game = Game(themes: [halloweenTheme, emojisTheme])
}
But I keep getting this or a similar error:
Cannot convert value of type 'Theme<Int>' to expected element type
'Array<Theme<Any>>.ArrayLiteralElement' (aka 'Theme<Any>')
Cannot convert value of type 'Theme<String>' to expected element type
'Array<Theme<Any>>.ArrayLiteralElement' (aka 'Theme<Any>')
And my mind is going crazy, I thought that by using [Theme<Any>] I would be able to have an array like this: [Theme<String>, Theme<Int>, Theme<Image>, ...] but it looks like not
Does anybody have a clue of what is going on here?
You can use a wrapper struct, basic example below. Note: if you need conformance to Codable, you need to implement encode/decode on your own.
struct Values<A> {
let value: A
}
struct Wrapper {
let wrappedValue: Values<Any>
}
class MyClass {
var wrappedValues: [Wrapper] = [Wrapper(wrappedValue: Values(value: "hello")), Wrapper(wrappedValue: Values(value: 1))]
}

How to create a pointer in Swift?

I'm working with Swift 3.
I would like to have this C syntax :
int myVar;
int *pointer = &myVar;
So modifying pointer or myVar does the same exact same thing.
Also I don't know if it makes any difference, but in my case myVar is an array containing elements of a class and pointer is a pointer to one element of this array.
The & also exists in Swift but can only be used as part of a parameter list (e.g. init, func, closure).
var i = 5
let ptr = UnsafeMutablePointer(&i)
print(ptr.pointee) // 5
// or
let ptr = UnsafeMutablePointer<Int>.allocate(capacity: 1)
ptr.initialize(to: 5)
// or with a closure
let ptr: UnsafePointer = { $0 }(&i)
(Assuming I understand what you're asking for....)
Try the following code in a playground. It should print "99" three times.
class Row {
var rowNumber = 0
}
var rows = [Row]()
let testRow = Row()
testRow.rowNumber = 1
rows.append(testRow)
let selectedRow = rows[0]
selectedRow.rowNumber = 99
print(testRow.rowNumber)
print(selectedRow.rowNumber)
print(rows[0].rowNumber)
By default, there's no copying of objects as part of an assignment statement. If it were a struct, that would be different.
Adding a bit for completeness:
If you want a similar effect with scalar values instead of objects, Swift supplies various types of wrappers.
let intPointer = UnsafeMutablePointer<Int>.allocate(capacity: 8) // Should be 1, not 8 according to comment re: docs
let other = intPointer
other.pointee = 34
print(intPointer.pointee)
(Warning: I haven't used these wrappers for anything except experimenting in a playground. Don't trust it without doing some research.)
Same example as #Phillip. But I used struct. In this example rows[0] won't change:
struct Row {
var rowNumber = 0
}
var rows = [Row]()
var testRow = Row()
testRow.rowNumber = 1
rows.append(testRow)
var selectedRow = rows[0]
selectedRow.rowNumber = 99
print(testRow.rowNumber) // prints 1
print(selectedRow.rowNumber) // prints 99
print(rows[0].rowNumber) // prints 1
There are no C style pointers (Unsafe Pointer) as the question asks however objects are shared by reference and structures are by value:
Swift assign, pass and return a value by reference for reference type and by copy for Value Type
structures are always copied when they are passed around in your code, but classes are passed by reference.
For example
How to have pointers/ references to objects
class Song {
init(title: String, image: String, file: String, volume: Float, queuePlayer: AVQueuePlayer, playerLooper: AVPlayerLooper?) {
self.title = title
self.image = image
...
}
var title: String
var image: String
...
}
var aSong = Song(title: "", image: "", ...)
var arrOfSongReferences: [Song] = [Song]()
arrOfSongReferences.append(aSong)
var ptrToASong: Song = aSong
aSong = nil
// Due to Swift garbage collection ARC (Automatic Reference Counting), we still have references to the original aSong object so it won't be deleted
If data is struct you cannot do this
struct Song {
var title: String
var image: String
...
}
var aSong: Song = Song(title: "", image: "", ...)
var copyOfASong: Song = aSong
Method
You can also pass by reference into a function
// this would be inside a class, perhaps Player. It doesn't have to be a static btw
static func playSound(_ sound: inout Song, volume: Float = 0.0) {
if (sound.playerLooper == nil) {
...
}
}
// usage
Player.playSound(sound: &aSong)

Generic Type array with UITableView and Realm

I am using realm for database. I have Favourite Object and History Object.
I want to show in TableViewController. However, I don't want to do duplicated code. For now, in FavouriteViewController , it has var favs: Results<OGFav>? and HistoryViewController, it has var history: Results<OGHistory>?
Most of the code are the same and different is data type.
Example: it only different like following
if let object:OGFav = self.favs?[indexPath.row] {
In some place , I use like
let fav:OGFav = favs[indexPath.row]
For History
if let object:OGHistory = self.history?[indexPath.row] {
History also use like below
let history:OGHistory = self.history[indexPath.row]
How can I clean the code ? I am using two viewcontroller and code are the same excepted OGFav and OGHistory.
Update:
OGFav and OGHistory have the same data.
class OGFav: Object {
dynamic var word = ""
dynamic var def = ""
}
class OGHistory: Object {
dynamic var word = ""
dynamic var def = ""
dynamic var updatedAt = NSDate()
}

How to get the string representation of a nested class

In my reflection library EVReflection I have the following problem when class definitions are nested (class within a class). Below is a worked out case which can be found as a unit test here and The location in the library itself where the code needs to change is Here
I need to get the Internal Swift string representation of a nested
class for a property which is an array of that nested class.
Below you can see a unit test where I am able to get the correct type for the property company that is an other object. It will output _TtCC22EVReflection_iOS_Tests13TestIssue114b10Company114 instead of Company114
When I try the same for the friends property my goal is that it outputs something like: Swift.Array<_TtCC22EVReflection_iOS_Tests13TestIssue114b7User114>
What do I have to do to get that?
As you can see in the test I have various assignments to the value valueType. None of these assignments work. I am only able to get an Array<User114> or an Swift._EmptyArrayStorage.
As you also can see in the test is that if I set a breakpoint and do a po in the output window I am able to get the correct output. So what code will accomplish the same in my code?
class TestIssue114b: XCTestCase {
class User114: EVObject {
var company: Company114 = Company114()
var friends: [User114] = []
}
class Company114: EVObject {
var name: String = ""
var address: String?
}
func testIssueNestedObjects() {
let x = User114()
print("type 1 = \(NSStringFromClass(type(of: x.company)))") // output = type 2 = _TtCC22EVReflection_iOS_Tests13TestIssue114b10Company114
print("type 2 = \(testIssueNestedObjects(x.friends))")
}
func testIssueNestedObjects(_ theValue: Any) -> String {
var valueType = ""
let mi = Mirror(reflecting: theValue)
valueType = NSStringFromClass(type(of: (theValue as! [NSObject]).getTypeInstance() as NSObject)) // NSObject
valueType = "\(type(of: theValue))" // Array<User114>
valueType = "\(mi.subjectType)" // Array<User114>
valueType = ObjectIdentifier(mi.subjectType).debugDescription //"ObjectIdentifier(0x0000000118b4a0d8)"
valueType = (theValue as AnyObject).debugDescription // <Swift._EmptyArrayStorage 0x10d860b50>
valueType = NSStringFromClass(type(of: theValue as AnyObject)) // Swift._EmptyArrayStorage
// set breakpont en enter this in output window: (lldb) po type(of: theValue)
// Ouput will be: Swift.Array<EVReflection_iOS_Tests.TestIssue114b.User114>
return valueType
}
}
Background info:
Actually the end goal is that I have to be able to create instances of the object that I can add to the array. Since the array property is only available as a result from a Mirror command the variable will be of type Any. I do have an extension for arrays in place that will return a new array element. however I am only able to get that when the Any is casted to Array<NSObject> and because of that my extension will return an NSObject. So I would like to get a string like Swift.Array<_TtCC22EVReflection_iOS_Tests13TestIssue114b7User114> I can then get the parts between <> and then create an instance for that using NSClassFromString.
String(reflecting: type(of: theValue))
update by Edwin Vermeer:
For the required conversion to the internal string representation i now have the following function (still in draft)
public class func convertToInternalSwiftRepresentation(type: String) -> String {
if type.components(separatedBy: "<").count > 1 {
// Remove the Array or Set prefix
let prefix = type.components(separatedBy: "<") [0] + "<"
var subtype = type.substring(from: prefix.endIndex)
subtype = subtype.substring(to: subtype.characters.index(before: subtype.endIndex))
return prefix + convertToInternalSwiftRepresentation(type: subtype) + ">"
}
if type.contains(".") {
var parts = type.components(separatedBy: ".")
if parts.count == 2 {
return parts[1]
}
let c = String(repeating:"C", count: parts.count - 1)
var rv = "_Tt\(c)\(parts[0].characters.count)\(parts[0])"
parts.remove(at: 0)
for part in parts {
rv = "\(rv)\(part.characters.count)\(part)"
}
return rv
}
return type
}