I have this classes:
class FacebookDB: Object {
dynamic var userName: String = ""
dynamic var profilePicture: String = ""
}
class TwitterDB: Object {
dynamic var userName: String = ""
dynamic var profilePicture: String = ""
}
class UserDB: Object {
dynamic var id = 0
override class func primaryKey() -> String {
return "id"
}
dynamic var userName: String = ""
dynamic var firstName: String = ""
dynamic var lastName: String = ""
dynamic var email: String = ""
dynamic var facebookAccount = FacebookDB?()
dynamic var twitterAccount = TwitterDB?()
}
After I put a value inside the userName
//user value come from facebook
let testUser = UserDB()
let realm = try! Realm()
testUser.FacebookAccount?.userName = user.userName
print(user.userName)
print(testUser.FacebookAccount?.userName)
Here is the output:
vivieng
nil
Why can't I print testUser.FacebookAccount?.userName?
BTW I can access every value from UserDB but none of FacebookDB nor TwitterDB, any idea?
Finally I found how to deal with this.
I need to create a value like that:
let facebookAccount = FacebookDB()
And then put everything I want inside:
facebookAccount.userName = user.userName
And finally copy the class inside the class:
testUser.FacebookAccount = facebookAccount
And voilĂ , that's work now!
Related
Im making a userProfile class in swift which includes the following code:
class userProfile {
var firstname: String!
var username: String!
var lastname: String!
var uid: String!
init(uid: String, dictionary: Dictionary<String, AnyObject>) {
self.uid = uid
if let username = dictionary["username"]as? String {
self.username = username
}
if let firstname = dictionary["firstname"]as? String {
self.firstname = firstname
}
if let lastname = dictionary["lastname"]as? String {
self.lastname = lastname
}
}
}
and i am trying to access the the attributes in another class with the following code:
var user: userProfile? {
didSet {
let fullName = userProfile?.firstname
firstname.text = fullName
}
}
But when i do this i get the following error and i don't know why:
Type 'userProfile?' has no member 'firstname'
how di i fix this?
Here is a fix (you tried to access class, but should be instance) :
var user: userProfile? {
didSet {
let fullName = user?.firstname // << here !!
firstname.text = fullName // I can't say if this valid
}
}
To avoid such ambiguity you should follow a rule to name classes in UpperCase, ie. in you case UserProfile.
I was finalizing my social media application and I am consistently having the same error regarding the "username" of my user during the launch process of the application (app is running, user is logged in, and the next view controller fails to come up and it crashes giving EXC_BAD_INSTRUCTION).
I was thinking it might be adata base problem as I had that with the the profile picture, however, the user name is in the database registered as a user with its email and password.
The code of the section the error is in:
import Foundation
import Firebase
import FirebaseDatabase
class Post {
private var _username: String!
private var _userImg: String!
private var _postImg: String!
private var _likes: Int!
private var _postKey: String!
private var _postRef: DatabaseReference!
var username: String
{
return _username
}
var userImg: String
{
return _userImg
}
var postImg: String {
get {
return _postImg
} set {
_postImg = newValue
}
}
var likes: Int {
return _likes
}
var postKey: String {
return _postKey
}
init(imgURl: String, likes: Int, username: String, userImg: String) {
_likes = likes
_postImg = imgURl
_username = username
_userImg = userImg
}
init(postKey: String, postData: Dictionary<String, AnyObject>){
_postKey = postKey
if let username = postData["username"] as? String {
_username = username
}
if let userImg = postData["userImg"] as? String {
_userImg = userImg
}
if let postImg = postData["imageUrl"] as? String{
_postImg = postImg
}
if let likes = postData["likes"] as? Int {
_likes = likes
}
_postRef = Database.database().reference().child("posts").child(_postKey)
}
func adjustLikes(addLikes: Bool) {
if addLikes {
_likes = likes + 1
} else {
_likes = likes - 1
}
_postRef.child("likes").setValue(_likes)
}
}
the line where the error occurs:
return _username
I am just really puzzled what the issue could be. I have looked at all the IBOutlets, as well as removing and adding new users. I would appreciate any help.
In the init(postKey:postData:) constructor it's not guaranteed that the _username property will be set. However, the public username property is of a non-optional type String. My assumption is that the username getter tries to forcefully unwrap a nil value.
Your username variable is a computed read only property which is returning value from variable _username:String!(Forced unwrapped value means can not be nil else crash ) .You need to be sure that your variable is not nil.
example when _username is not nil-:
class Foo{
var _username:String = "tushar"
var username: String
{
return _username
}
}
var object = Foo()
print(object.username)
Example when your variable can get nil value-:
class Foo{
var _username:String!
var username: String
{
print(_username)
return _username
}
}
var object = Foo()
print(object.username)
If variable has no value in it that's a crash
I'm trying to save my array of objects in Realm, but Realm appears to be saving only the last object.
This is my model class:
class ContactEntry: Entry {
dynamic var contactId: Int = 0
dynamic var email: String? = ""
dynamic var phone: String? = ""
dynamic var building: String? = ""
dynamic var room: String? = ""
dynamic var contactDescription: String? = ""
dynamic var worktime: String? = ""
dynamic var address: String? = ""
dynamic var personEntries: PersonEntry?
}
This is the base class:
class Entry: Object {
dynamic var id: Int = 0
override static func primaryKey() -> String? { return "id" }
}
This is code I'm using for saving:
func createOrUpdate(entities: [Entity]) {
let entries = toEntries(entities)
let realm = try! Realm()
try! realm.write {
realm.add(entries, update: true)
}
}
func toEntry(entity: Contact) -> ContactEntry {
let entry = ContactEntry()
entry.contactId = entity.id
entry.email = entity.email
entry.phone = entity.phone
entry.building = entity.building
entry.room = entity.room
entry.contactDescription = entity.description
entry.worktime = entity.worktime
entry.address = entity.address
entry.personEntries = personDAO.toEntry(entity.person)
return entry
}
func toEntry(entity: Person) -> PersonEntry {
let entry = PersonEntry()
entry.personId = entity.id
entry.firstname = entity.firstname
entry.middlename = entity.middlename
entry.lastname = entity.lastname
entry.photo = entity.photo
return entry
}
I think that it may be because I have relationship in my model, but I'm not sure why they'd be a problem.
I'm using them as described in the Realm documentation.
Hello i'm beginner in Swift programming.
Im trying to make an app with login, which will take user and pass data, parse to JSON and send it to server.
I need to able that an user take only two values: user and pass from field, parse the whole object into JSON and send post request.
Before all that i have problem with making User struc for mapping: error is on the end of init " Return from initializer without initialising all sorted properties".
struct User: Mappable{
private var _username: String!
private var _password: String!
private var _firstname: String
private var _lastname: String
var username: String{
get{
return _username
}set(username){
self._username = username
}
}
var password: String{
get{
return _password
}set(password){
self._password = password
}
}
var firstname: String{
get{
return _firstname
}set(firstname){
self._firstname = firstname
}
}
var lastname: String{
get{
return _lastname
}set(lastname){
self._lastname = lastname
}
}
init(username: String, password: String){
self._username = username
self._password = password
}
init?(_ map: Map) {
}
mutating func mapping(map: Map) {
username <- map["USERNAME"]
password <- map["PASSWORD"]
firstname <- map["FIRST_NAME"]
lastname <- map["LAST_NAME"]
}
}
I know i have to learn so much, but right now don't have time, so must finish this. I'm trying to learn as much as it possible.
Thank you so much
All of your non-optional properties must be initialized prior to exiting your init methods. Setting them to empty strings (or whatever) should eliminate the error.
init(username: String, password: String){
self._username = username
self._password = password
self._firstname = ""
self._lastname = ""
}
init?(_ map: Map) {
self._username = ""
self._password = ""
self._firstname = ""
self._lastname = ""
}
Alternately you could make those properties optional.
I have class User.swift:
import UIKit
#objc(User)
class User: DBObject {
dynamic var Sync: NSNumber?
dynamic var Login: NSString?
dynamic var Password: NSString?
dynamic var id_Worker: NSNumber?
static func authorize(login: String, password: String) -> Bool{
var query = User.query().whereWithFormat("login = %# and password = %#", withParameters: [login, password.md5])
var resSet = query.fetch() as DBResultSet
if resSet.count > 0 {
var user = resSet[0] as! User
}
if resSet.count > 0{
return true
} else {
return false
}
}
}
I want to retrieve User object from DBResultSet. But under the debbuger I get this strange construction:
How can I retrieve object User from DBResultSet?