PFUser to custom model - swift

I have a model I built for my PFUser from Parse:
import Foundation
import Parse
class TradeUser : PFUser {
override class func initialize() {
self.registerSubclass()
}
var userID : String {
get {return objectForKey("objectId") as! String}
set { setObject(newValue, forKey: "objectId") }
}
var emailAddress : String {
get {return objectForKey("email") as! String}
set { setObject(newValue, forKey: "email") }
}
var firstName : String {
get {return objectForKey("firstName") as! String}
set {setObject(newValue, forKey: "firstName")}
}
var lastName : String {
get {return objectForKey("lastName") as! String}
set {setObject(newValue, forKey: "lastName")}
}
var primaryQueue : String {
get {return objectForKey("primaryQueue") as! String}
set {setObject(newValue, forKey: "primaryQueue")}
}
var image : PFFile {
get { return self["profileImage"] as! PFFile }
set { self["profileImage"] = newValue }
}
}
But when I query to get a user, and try to cast it as that, I get a "Could not cast value of type 'PFUser' (0x107555928) to 'ShiftSwap.TradeUser' (0x10754e910)."
I thought that TradeUser would be the same value type as PFUser, since it is a PFUser cast? I'm a little confused, and any clarification would be appreciated!
EDIT:
#IBAction func chatBarButton(sender: AnyObject) {
let postingUser = self.object?.objectForKey("userID") as! String
let tradeUserQuery = PFUser.query()
tradeUserQuery?.whereKey("objectId", equalTo: postingUser)
let trader = tradeUserQuery?.getFirstObject() as! TradeUser
var chatVC = MessagesViewController()
chatVC.currentUser = TradeUser.currentUser()
print(TradeUser.currentUser())
chatVC.otherUser = trader
self.navigationController?.pushViewController(chatVC, animated: true)
}

You are forcing a downcast which is impossible. You cannot downcast an instance of a base class PFUser to a derived one TradeUser.

Related

Value of a child from mirror introspection not conformed to protocol anymore

I am trying to understand swift's inflection capabilities.
I have a parent Passport class whose child (User) implements a protocol Clonable, however when introspecting the child value, it fails the check child.value is Clonable.
Can someone explain this?
extension Clonable {
func clone() -> Self? {
if let me = self as? SimpleInit {
let new = type(of: me).init()
let mirror = Mirror(reflecting: self)
for child in mirror.children {
if let kp = child.label, let new = new as? NSObject {
if child.value is Clonable, let value = child.value as? Clonable { // this should be true
print("cloning \(child.value) for keypath \(kp)")
new.setValue(value.clone(), forKeyPath: kp)
} else {
print("not cloning \(child.value) for keypath \(kp)")
new.setValue(child.value, forKeyPath: kp)
}
}
}
return new as? Self
}
return nil
}
}
class Passport: NSObject, Clonable, SimpleInit, CustomReflectable {
var customMirror: Mirror {
return Mirror(self, children: ["user": user])
}
#objc var user: User?
required override init() {
}
func printMe() {
user?.printMe()
}
}
class User: NSObject, Clonable, SimpleInit, CustomReflectable {
var customMirror: Mirror {
return Mirror(self, children: ["name": name])
}
#objc var id: Int
#objc var name: String?
required override init() {
print("init user")
id = Int(arc4random())
}
func printMe() {
print("id \(id) name \(name)")
}
}
let passport = Passport()
passport.user = User()
passport.user?.name = "John"
let clone = passport.clone()
passport.printMe()
clone?.printMe()
This is the output:
init user // should be called second time when user gets cloned.
not cloning Optional(<__lldb_expr_92.User: 0x6000039d6420>) for keypath user
id 2046302380 name Optional("John")
id 2046302380 name Optional("John")

Swift Firebase get Data to Class Object

I want to get the following structure (screenshot of Firebase Database):
In chats I have the id of the chat. There are the users with the child userid and the values of id and name.
At first I look for the chats which a user have and want to get then the details of the chatId (users with their id and name)
I have the following class in Swift:
class Chat {
var chatId: String!
var userIds: [String]!
var userNames: [String]!
}
I have the following code to get the details, but I get not the userIds or userNames from the chatId:
func getChatsFromFirebase() {
self.ref = Database.database().reference()
self.ref?.child("users").child(userdefaults.getUserId()).child("chats").observe(.childAdded, with: { (snapshot) in
let chat = Chat()
chat.chatId = snapshot.key
chat.userIds = []
chat.userNames = []
//print(chat.chatId)
for i in 0..<self.chats.count {
let usersRef = self.ref.child("chats").child(self.chats[i].chatId).child("users").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
for userid in value!.allKeys as! [String] {
let usersdetailsRef = self.ref.child("chats").child(self.chats[i].chatId).child("users").child(userid).queryOrdered(byChild: "name").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
//print(value)
let id = value?["id"] as? String ?? ""
let name = value?["name"] as? String ?? ""
//print( id + ": " + name)
chat.userIds.append(id)
chat.userNames.append(name)
})
}
})
}
self.chats.append(chat)
self.tableView.reloadData()
})
}
I am very new to the Firebase topic. Can someone help me here?
Thanks.
Well You need to change your datamodel first. You dont need to store id value in , 12345 in this case. you can already fetch the key. Also, in /users/chats, you just can just save the chat id as either chat1 : IBDrbfku887BLIY or IBDrbfku887BLIY : true. You can always fetch them through value or the key respectively.
And in your chat document, you just need to reference the user id, i.e just get them and store them as user1 and user2. You can add more users if your usecase requires more.
Reconfigure your Data Model as follows.
Now You need 2 Objects Users and Chats as follows :
Users.swift
class User : NSObject {
private var _name: String!
private var _username: String!
private var _userid: String!
private var _userRef: DatabaseReference!
var name: String! {
get {
return _name
} set {
_name = newValue
}
}
var username : String! {
get {
return _username
} set {
_username = newValue
}
}
var userid: String! {
get {
return _userid
} set {
_userid = newValue
}
}
var userRef: DatabaseReference! {
get {
return _userRef
} set {
_userRef = newValue
}
}
init(userid: String, userData: Dictionary<String, Any>){
self._userid = userid
_userRef = Database.database().reference().child(_userid)
if let username = userData["username"] as? String {
self._username = username
}
if let name = userData["name"] as? String {
self._name = name
}
}
}
Chats.swift
class Chat : NSObject {
private var _chatid: String!
private var _user1: String!
private var _user2: String!
private var _chatRef: DatabaseReference!
var user1: String! {
get {
return _user1
} set {
_user1 = newValue
}
}
var user2 : String! {
get {
return _user2
} set {
_user2 = newValue
}
}
var chatid: String! {
get {
return _chatid
} set {
_chatid = newValue
}
}
var chatRef: DatabaseReference! {
get {
return _chatRef
} set {
_chatRef = newValue
}
}
init(chatid: String, chatData: Dictionary<String, Any>){
self._chatid = chatid
_chatRef = Database.database().reference().child(_chatid)
if let user = chatData["users"] as? Dictionary<String, Any> {
if let user1 = user["user1"] as? String {
self._user1 = user1
}
if let user2 = user["user2"] as? String {
self._user2 = user2
}
}
}
}
The major issue/or an overlooked issue here is the type of the data. In the /users, you id 12345 will be of type String. But when you fetch the same from /chats, it returns as Int. This downloads the value but never converts it. Always take care while seeding/testing your data.
To fetch the user's credentials just reference that through another query. This is what you can do :
var allUsers = [User]()
var allChats = [Chat]()
func viewDidLoad() {
super.viewDidLoad()
fetchAllChats()
}
func getUser(from userId: String, completion: #escaping (User) -> Void) {
Database.database().reference().child("users").child(userId).observeSingleEvent(of: .value, with: { snapshot in
if let datasnap = snapshot.value as? Dictionary<String, Any> {
let user = User(userid: userId, userData: datasnap)
completion(user)
}
})
}
func fetchAllChats() {
Database.database().reference().child("chats").observeSingleEvent(of: .value, with: { snapshot in
allChat.removeAll()
if let snapshot = snapshot.value as? Dictionary<String, Any> {
for snap in snapshot {
if let chatd = snap.value as? Dictionary<String, Any> {
let chat = Chat(chatid: snap.key, chatData: chatd)
self.allChats.append(chat)
}
}
}
// collectionview.reloadData() <--------- only if required.
})
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let chatData = allChats[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellId, for: indexPath) as! Cell
getUser(from: chatData.user1) { user in
cell.label.text = user.usernme
}
return cell
}

Can't cast a value from a subclass Swift

I made a lot of research but I didn't find an answer to my question. Others talk about basic issues with Swift classes. Still I have an issue with my own classes. I also read courses about classes but it didn't help me.
I have two classes; one of them inherit from the other.
Here is my classes code :
class GlobalUser {
var uid: String!
var publicName: String!
var pushID: String!
var firstName: String!
var lastName: String!
var example1: [String:String]!
var fullName: String! {
get {
return firstName + " " + lastName
}
}
init(document: DocumentSnapshot) {
guard let data = document.data() else {
print("Missing user information during initialization.")
return
}
self.uid = document.documentID
self.publicName = (data["publicName"] as? String)!
self.pushID = (data["pushID"] as? String)!
self.example1 = (data["example1"] as? [String : String])!
let name = data["name"] as? [String:String]
self.firstName = (name!["firstName"])!
self.lastName = (name!["lastName"])!
}
}
class InterestingUser: GlobalUser {
var code: Int?
var example: [String:String]?
var number: Int! {
get {
return example.count
}
}
override init(document: DocumentSnapshot) {
super.init(document: document)
}
}
And then I try to cast a GlobalUser to a InterestingUser like this :
if let interestingUser = user as? InterestingUser {
...
}
But this cast always fails...
Any idea? Thanks in advance for your help.
The error you're experiencing is due to this statement from your question: 'And then I try to cast a GlobalUser to a InterestingUser like this...' and is due to inheritance.
Your GlobalUser class is the superclass. Your InterestingUser is a subclass of your GlobalUser.
So your InterestingUser class 'knows' about the GlobalUser because it is it's parent and you can cast InterestingUser as? GlobalUser but not the other way around.
Example:
if let interstingUser = InterestingUser() as? GlobalUser {
// this will succeed because InterestingUser inherits from GlobalUser
}
if let globalUser = GlobalUser() as? InterestingUser {
// this will fail because GlobalUser is not a subclass of InterestingUser
}
Here's some playground code for you to test with:
class GlobalUser {
}
class InterestingUser: GlobalUser {
}
class Demo {
func comparison() {
let interesting = InterestingUser()
let global = GlobalUser()
if let intere = interesting as? GlobalUser {
print("Interesting is global as well")
}
if let global = global as? InterestingUser {
print("Global is interesting")
}
}
}
let demo = Demo()
demo.comparison()
// prints 'Interesting is global as well'

SWIFT4 Contextual type 'FPChat!.Type' cannot be used with dictionary literal

I need to initialize an object, and pass it through a prepareforsegue to another class.
Last line of the code below throws "Contextual type 'FPChat!.Type' cannot be used with dictionary literal"
if (segue.identifier == "chatmessages") {
let vc = segue.destination as! FPChatMessageViewController
//vc.currentChat = fPChat
}
}
fPchat = FPChat?
// Start the Chat
#IBAction func Chat(_ sender: UIButton) {
// Create a new entry in chats. This variable is passed with prepareforsegue
let chatRef = ref.child("chats").childByAutoId()
let chatId = chatRef.key
//fPchat = FPChat?
let fPchat = FPChat.currentChat(currentChatID: chatId)
Below chat class:
import Firebase
class FPChat {
var chatID = ""
var chatDate: Date!
var text = ""
var messages: [FPChatMessage]!
var author: FPUser!
var mine = true
// Calling FPChat.currentChat(id) I have back the FPChat object
static func currentChat(currentChatID: String) -> FPChat {
return FPChat(chatID: currentChatID)
}
private init(chatID: String) {
self.chatID = chatID
}
init(snapshot: DataSnapshot, andMessages messages: [FPChatMessage]) {
guard let value = snapshot.value as? [String: Any] else { return }
self.chatID = snapshot.key
if let text = value["text"] as? String {
self.text = text
}
guard let timestamp = value["timestamp"] as? Double else { return }
self.chatDate = Date(timeIntervalSince1970: (timestamp / 1_000.0))
guard let author = value["author"] as? [String: String] else { return }
self.author = FPUser(dictionary: author)
self.messages = messages
self.mine = self.author.userID == Auth.auth().currentUser?.uid
}
}
What I am doing wrong?

Casting AnyObject to Specific Class

I'm using socket.io Swift Library. With the following line of code,
socket.on("info") { (dataArray, socketAck) -> Void in
let user = dataArray[0] as? User
print(user._id)
}
dataArray[0] is a valid object but user appears to be nil after casting.
Since dataArray[0] returns as an AnyObject,
how can i cast AnyObject to User Object?. Or somehow manage to do what i want with a different approach?
Since after this line
let user = dataArray[0] as? User
you have a nil value inside user it means that you don't have a User value at the first position of dataArray.
Since dataArray comes from a server (as I guess) it probably contains a serialized version of User.
Now we really need to know what really dataArray[0] is. However...
if dataArray[0] contains NSData
In this case try this
let json = JSON(dataArray[0] as! NSData)
let user = User(json:json)
You need to create a constructor that accept AnyObject and read data in it.
I guess in this case dataArray[0] is an JSON Object.
class User {
init(data: [String: AnyObject]) {
username = data["username"] as? String ?? ""
}
}
This is how i manage mine:
// Structure used as parameter
struct InfoStruct {
var nome: String = ""
var sobrenome:String = ""
var nascimentoTimestamp: NSNumber = 0
init() {
}
// Struct to AnyObject
func toAnyObject() -> Any {
var dic = [String:AnyObject?]()
if (nome != "") { dic["nome"] = nome as AnyObject }
if (sobrenome != "") { dic["sobrenome"] = sobrenome as AnyObject }
if (nascimentoTimestamp != 0) { dic["nascimentoTimestamp"] = nascimentoTimestamp as AnyObject }
return dic
}
// AnyObject to Struct
func fromAnyObject(dic:[String:AnyObject]) -> InfoStruct {
var retorno = InfoStruct()
if (dic["nome"] != nil) { retorno.nome = dic["nome"] as? String ?? "" }
if (dic["sobrenome"] != nil) { retorno.sobrenome = dic["sobrenome"] as? String ?? "" }
if (dic["nascimentoTimestamp"] != nil) { retorno.nascimentoTimestamp = dic["nascimentoTimestamp"] as? NSNumber ?? 0 }
return retorno
} }
// User class
class Usuario: NSObject {
var key: String
var admin: Bool
var info: InfoStruct // Struct parameter
init(_ key: String?) {
self.key = key ?? ""
admin = false
info = InfoStruct() // Initializing struct
}
// From Class to AnyObject
func toAnyObject() -> Any {
var dic = [String:AnyObject?]()
if (key != "") { dic["key"] = key as AnyObject }
if (admin != false) { dic["admin"] = admin as AnyObject }
dic["info"] = info.toAnyObject() as AnyObject // Struct
return dic
}
// From AnyObject to Class
func fromAnyObject(dic:[String:AnyObject]) -> Usuario {
let retorno = Usuario(dic["key"] as? String)
if (dic["key"] != nil) { retorno.key = dic["key"] as? String ?? "" }
if (dic["admin"] != nil) { retorno.admin = dic["admin"] as? Bool ?? false }
if (dic["info"] != nil) { retorno.info = InfoStruct.init().fromAnyObject(dic: dic["info"] as! [String : AnyObject]) } // Struct
return retorno
} }
// Using
let dao = FirebaseDAO.init(_type: FirebaseDAOType.firebaseDAOTypeUser)
dao.loadValue(key: uid) { (error, values:[String : AnyObject]) in
if error == nil {
let user = Usuario(values["key"] as? String).fromAnyObject(dic: values)
}
}
I hope it helps!