Swift - Declare Struct in two different files - swift

I just approached Swift and I'm encountering some problems...
I have a class which helps me to fetch some results
class ExpencesOperations {
struct ExpencesByDate {
var day: String!
var expence: [PFObject]
}
var expencesByDateArray = [ExpencesByDate]()
func getExpencesByDate(expences: [PFObject]) -> [ExpencesByDate] {
..... my function
return expencesByDateArray
}
}
When I call it in my TableViewController I don't know how to redeclare the struct ExpencesDate!
class HomeTableViewController: UITableViewController {
var myCurrency = MyCurrency()
var expencesOperations = ExpencesOperations()
var expencesByDateArray = [ExpencesOperations.ExpencesByDate]() /* Not Working */
func fetchExpences() {
expencesByDateArray = self.expencesOperations.getExpencesByDate(someInput) /* here i get the error */
}

Put the struct outside of your class scope and it should be visible to other files as well.
struct ExpencesByDate {
var day: String!
var expence: [PFObject]
}
class ExpencesOperations {
var expencesByDateArray = [ExpencesByDate]()
func getExpencesByDate(expences: [PFObject]) -> [ExpencesByDate] {
..... my function
return expencesByDateArray
}
}

You need to provide arguments for proper initialization of the ExpencesByDate struct (I think you meant ExpenSes, right?)
For example:
var expencesByDateArray = [ExpencesOperations.ExpencesByDate(day: "Friday", expence: [PFObject]())]
And regarding:
...getExpencesByDate(someInput) /* here i get the error */
The function you created expects an array:
func getExpencesByDate(expences: [PFObject]) -> [ExpencesByDate]
It's not clear from your question what someInput is. Is that an array?
As you posted in the question, it will always fail.
Plus, what are the compiler error messages?

Related

Executiong closure on array modification

I have the following code:
class Note: NSObject {
}
struct Global {
static var notes: Array<Note> = [] {
didSet {
print("hi")
}
}
}
This prints "hi" if I add or remove an item from the array or if I do
Global.notes = []
Is there a way to print("hi") every time when one of the Note objects in the array is modified?
Thanks for your answers
Without changing the class to a struct, I have two basic ways to handle this.
This is the object you asked about
class Note: NSObject {
}
struct Global {
static var notes: Array<Note> = [] {
didSet {
print("hi")
}
}
}
Wrap Notes in a wrapper that is a struct to get the struct behavior.
extension Note {
struct Wrapper { let note: Note }
}
extension Global {
static var wrappedNotes = [Note.Wrapper]() {
didSet {
print("hi")
}
}
}
Global.wrappedNotes.append(Note.Wrapper(note: Note()))
Global.wrappedNotes[0] = Note.Wrapper(note: Note())
Global.wrappedNotes.remove(at: 0)
The other way is to create a note manager to wrap access to the array.
class NoteManager {
subscript(index: Int) -> Note {
get {
return values[index]
}
set {
defer { onUpdate() }
values[index] = newValue
}
}
func append(_ newNote: Note) {
defer { onUpdate() }
values.append(newNote)
}
func remove(at index: Int) -> Note {
defer { onUpdate() }
return values.remove(at: index)
}
private func onUpdate() {
print("hi")
}
private var values = [Note]()
}
extension Global {
static var managedNotes = NoteManager()
}
Global.managedNotes.append(Note())
Global.managedNotes[0] = Note()
Global.managedNotes.remove(at: 0)
As per #staticVoidMan comment , If you make your model , a struct, rather than a class, then the property observer didSet will work for your Note model's own properties as well.
import Foundation
struct Note {
var name: String
}
struct Global {
static var notes: Array<Note> = [] {
didSet {
print("hi")
}
}
}
Global.notes.append(Note(name: "Shubham"))
Global.notes.append(Note(name: "Bakshi"))
Global.notes[0].name = "Boxy"
This will print the following on the console :
hi
hi
hi
Swift Array is a struct, and structs are value-type which means they change completely when elements are added/removed/replaced. Hence when you add/remove/replace a Note, the didSet property observer gets called as the array has been set again.
However, as per you question:
Is there a way to print("hi") every time when one of the Note objects in the array is modified?
By this I am assuming that you want to do something when an element within this array is accessed and an internal property is modified.
This would have been fine if you were dealing with only value-type objects, i.e. had your Note object also been a struct, then changing anything inside one Note would have caused the array to change as well.
But your Note object is a class, i.e. reference-type, and stays as the same object even if it's internal elements change. Hence your array doesn't need to update and didSet does not get called.
Read: Value and Reference Types
KVO Solution:
Now... Since your Note is subclassing NSObject, you can use the KVO concept
As per the following working example, we observe only one property of the Note class.
If you want to observe more properties then you will need to observe those many more keypaths.
Example:
class Note: NSObject {
#objc dynamic var content = ""
init(_ content: String) {
self.content = content
}
}
class NoteList {
var notes: [Note] = [] {
didSet {
print("note list updated")
//register & save observers for each note
self.noteMessageKVOs = notes.map { (note) -> NSKeyValueObservation in
return note.observe(\Note.content, options: [.new, .old]) { (note, value) in
print("note updated: \(value.oldValue) changed to \(value.newValue)")
}
}
}
}
//array of observers
var noteMessageKVOs = [NSKeyValueObservation]()
}
let list = NoteList()
list.notes.append(Note("A")) //note list updated
list.notes.append(Note("B")) //note list updated
list.notes[0].content = "X" //note updated: A changed to X
list.notes[1].content = "Y" //note updated: B changed to Y
Notes:
NSObject is required for KVO
#objc dynamic is required to make a property observable
\Note.message is a keypath
noteMessageKVOs are required to keep the observers alive

Swift, Firebase: `setValue` giving error "AnyObject cannot be used with Dictionary Literal"

I am experiencing an error Contextual type AnyObject cannot be used within dictionary literal in that func addPet down there, while trying to populate database with the newPet argument constituents in a dictionary.
import Foundation
import Firebase
struct Pet {
var name:String?
var type:String?
var age:Int?
var feedingList:[String]
var walkingList:[String]
}
struct User {
var currentId:String?
var numberOfPets:Int?
var pets:[Pet]
}
class petBrain {
var reff = FIRDatabaseReference()
var currentUser:User = User(currentId: "",numberOfPets: 0,pets: [])
init(){
self.reff = FIRDatabase.database().reference()
}
func setUserId(cId:String?){
self.currentUser.currentId = cId
}
func addPet(newPet:Pet) {
self.reff.child("pets").childByAutoId().setValue(["name":newPet.name, "type":newPet.type, "age":newPet.age, "fList":newPet.feedingList, "wList":newPet.walkingList])
}
}
I have already done this in other viewController, similarly for users and its working fine in dictionary shape (producing no error)
let em = emailTextField.text!
let us = usernameTextField.text!
...
else {
print("User created! Loging in.")
self.login()
// adding user to DB of users
self.ref.child("users").child(user!.uid).setValue(["email":em, "username":us])
}
What did i do wrong in the pets case? its maybe due to struct, or struct element types? Are those two structs well defined?
? is used if the value can become nil in the future.
! is used if it really shouldn't become nil in the future, but it needs to be nil initially.
See the problem is Swift is a strictly typed language, if you declare a variable ? you are saying that it's of type nil. So a dictionary cant tell what type of a value would it be storing....
var myVar : String? // Look myVar, you and your type is completely nil as of now
var myVar : String! // Look myVar, your value is nil as of now but your type is certainly of String
Just change your code to this:-
struct Pet {
var name:String!
var type:String!
var age:Int!
var feedingList:[String]
var walkingList:[String]
}
struct User {
var currentId:String?
var numberOfPets:Int?
var pets:[Pet]
}
class petBrain {
var reff = FIRDatabaseReference()
var currentUser:User = User(currentId: "",numberOfPets: 0,pets: [])
init(){
self.reff = FIRDatabase.database().reference()
}
func setUserId(cId:String?){
self.currentUser.currentId = cId
}
func addPet(newPet:Pet) {
let dict = ["name":newPet.name, "type":newPet.type, "age":newPet.age, "fList":newPet.feedingList, "wList":newPet.walkingList]
self.reff.child("pets").childByAutoId().setValue(dict)
}
}

Swift - A function with no parameters with return value

I am learning Swift and am writing a basic card game init function where I want to use a function to setup some decks.
Swift keeps complaining that I'm missing an argument in parameter #1, but there aren't any parameters, nor am I wanting any.
Game class is as follows
class Game
{
// MARK: ** Private vars **
private var gameState: GameState?
private var playerOnTurn: Player?
private var seedCash:Int?
// MARK: ** Public vars **
lazy var players = [Player]()
var chequesDeck:Deck = Deck()
var propertiesDeck:Deck = Deck()
init()
{
self.gameState = .Initialize
self.playerOnTurn = nil // No player on turn when game is initialized
self.seedCash = kInitialSeedCash
}
func setup(numberOfPlayers:Int)
{
// Create decks of properties and cheques
self.propertiesDeck = Deck.createProperties()
self.chequesDeck = Deck.createCheques()
}
}
Deck class is as follows
// Deck of cards
// Two deck types in the game - (1) Properties & (2) Cheques
class Deck
{
private var cards:[Card] = [] // Empty Array
// #return: An array of cards
func createProperties() -> [Card]
{
var propertyDeck:[Card] = []
// TODO: - Needs Local JSON reader
let prop1 = Card.init(name:"Cardboard box", value:1)
propertyDeck.append(prop1)
let prop2 = Card.init(name:"Outhouse", value:2)
propertyDeck.append(prop2)
let prop3 = Card.init(name:"Outhouse", value:3)
propertyDeck.append(prop3)
return propertyDeck
}
// #return: An array of cards
func createCheques() -> [Card]
{
var chequeDeck:[Card] = []
// create 2 copies of each card, but skip 1s
for var i:Int = 0; i<=15; i++
{
if (i != 1)
{
let chequeCard = Card.init(name: "Cheque", value: i * 1000)
chequeDeck.append(chequeCard)
}
}
return chequeDeck
}
func addCard()
{
}
func shuffle()
{
}
}
Deck() is a class
func setup() {
var propertiesDeck:Deck = Deck()
// Create property deck
self.propertiesDeck = Deck.createProperties()
}
// Deck.createProperties file
// #return: An array of cards
func createProperties() -> [Card]
{
var propertyDeck:[Card] = []
let prop1 = Card.init(name:"Penthouse", value:1)
propertyDeck.append(prop1)
return propertyDeck
}
But Swift keeps complaining that;
Missing argument for parameter #1 in call
But there aren't any arguments or parameters.
Perhaps I'm doing something wrong/silly?
This error would generally say that Deck is expecting some constructor parameters. Could you please post your Deck class so I can see if there are any?
Also some more suggestions. You seem to be creating the Deck variable propertiesDeck, but then statically accessing createProperties by stating Deck.createProperties(). Should you not be calling propertiesDeck.createProperties()? Also createProperties is returning an Array of the Card object, but propertiesDeck is a Deck class.
Since you're accessing your function like this:
Deck.createProperties()
you probably want a static method instead:
static func createProperties() -> [Card] {
...
}
Which shouldn't give you an error anymore.
Another way to make it work is by calling createProperties() on your already defined Deck like this (not recommended):
self.propertiesDeck = propertiesDeck.createProperties()
The reason for the missing parameter comes from the fact that methods take the class instance as their first parameter, so you could actually call it like Deck.createProperties(propertiesDeck)().
I believe you've got some other flaws in your code, I will try to make a better example for you:
struct Card {
let name : String
let value : Int
}
class Deck {
var cards : [Card]
init() {
cards = [
Card(name: "Penthouse", value: 1)
]
}
}
Try This,
func createProperties() -> [Card]
{
var propertyDeck:[Card] = []
let prop1 = Card(name:"Penthouse", value:1)
propertyDeck.append(prop1)
return propertyDeck
}
If this does not works then show the code for the class Deck and Card.
We can't answer your question without the complete code. We need the code of your Deck class.
But you can try class funcs...
public class Cards {
class func createProperties() -> [Card]
{
var propertyDeck:[Card] = []
let prop1 = Card.init(name:"Penthouse", value:1)
propertyDeck.append(prop1)
return propertyDeck
}
} 
You can call the function with:
Cards.createProperties()

Swift Get value from function

I have two class and i would like to get a variable with the value (the variable is in a function) to my second class :
public class StreamPlayer {
class var sharedInstance : StreamPlayer{
struct Static {
static let instance : StreamPlayer = StreamPlayer()
}
return Static.instance
}
public var intermediate = NSString()
func metaDataUpdated(metaData : NSString){
var result : String = ""
var listItems = metaData.componentsSeparatedByString(";") as [String]
if (listItems.count > 0){
var containerName = listItems[0]
result = "StreamTitle=\'([^\"]*)\'".matchesForRegexIn(containerName, atRangeIndex: 1)
self.intermediate = result
}
}
}
and the second class
class RadioViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
println(test + StreamPlayer.sharedInstance.intermediate)
}
}
The problem is that the var intermediate doesn't change and don't get the value of result (in my first class)
I've copied your StreamPlayer class code into a playground. I've just commented out the matchesForRegexIn method as it seems it's your String extension so my code looks like this:
public class StreamPlayer {
class var sharedInstance : StreamPlayer{
struct Static {
static let instance : StreamPlayer = StreamPlayer()
}
return Static.instance
}
public var intermediate = String()
func metaDataUpdated(metaData : NSString){
var result : String = ""
let listItems = metaData.componentsSeparatedByString(";") as [String]
if (listItems.count > 0){
// var containerName = listItems[0]
result = "StreamTitle=\'([^\"]*)\'" //.matchesForRegexIn(containerName, atRangeIndex: 1)
intermediate = result
}
}
}
// calling the method to make sure intermediate gets updated
StreamPlayer.sharedInstance.metaDataUpdated("asd")
// check if it got updated
print(StreamPlayer.sharedInstance.intermediate)
The last line prints StreamTitle=\'([^\"])\'* so all is good. Just make sure to call StreamPlayer.sharedInstance.metaDataUpdated before checking intermediate
PS. I'm really not sure what you're trying to achieve by sharing intermediate results from a function to the outside world but it feels off. Think about splitting metaDataUpdated method into two methods maybe?
PPS. metaDataUpdated is a really bad name for a function
PPPS. If I were you I'd declare intermediate as String?
self.intermediate is a NSString while result is a String
Try
self.intermediate = result as NSString

How can I determine what is the type of generic parameter type in function in Swift?

I have a protocol and 2 struct that conform to that protocol and I have a function that take a generic (which constraints to protocol). In the function, I have to get some data based on the type of the argument. I try to find a way to do this but I'm still not sure which way is the most suitable for this scenario.
protocol Data {
var id: String { get }
}
struct File: Data {
var id: String
var size: Int
}
struct Folder: Data {
var id: String
var color: Int
}
class Observer {}
private var callbacksOfFile: [String: ObserverSet<File>] = [:]
private var callbacksOfFolder: [String: ObserverSet<Folder>] = [:]
func subscribeUpdateForData<T: Data>(data: Data, withCallback callback: (T) -> Void) -> Observer {
if let file = data as? File, let callbacks = callbacksOfFile[data.id] {
callbacks.add(callback) // Error
} else if let folder = data as? Folder, let callbacks = callbacksOfFolder[data.id] {
callbacks.add(callback) // Error
}
}
What is the best way to work on this case? Now I use function overloading instead of Generic but I ask you in case there is a better way for this scenario.
Thank you.
You many not need your individual callbackOf{File,Folder} distinction as they are, after all, both subtypes of Data. Perhaps
class Observer {
var callbacks : [((Data) -> Void)] : []
func addCallback (callback: (Data) -> Void) {
callbacks.append(callback)
}
func invokeCallbacks () {
/* ... */
}
}
If all you need is that different callback code is invoked on File and Folder types, then you do this within your structures. Like this:
protocol Data {
var id: String { get }
func processData ();
}
struct File: Data {
var id: String
var size: Int
func processData () {
/* implement for File */
}
}
struct Folder: Data {
var id: String
var color: Int
func processData () {
/* implement for Folder */
}
}
and then call 'processData()` appropriately.