Maraz Reis Maraz Reis - 14 days ago 5
Swift Question

Swift 3, Cant create more than one function in viewcontroller ?

I am trying to make a Sign in / Sign up screen with Firebase. And i created a swift class for send data to Firebase and i called the class "DataService". I have also Sign in and Sign up classes. In the "DataService" class i cant create more than one function, i am getting error like Value of type DataService has no member "Sign up" when i am trying to create a sign up function. But the other function works fine. I can have just only one function in this class(DataService).

DATASERVICE class

import Foundation
import Firebase
import FirebaseAuth
import FirebaseStorage

let rootRef = FIRDatabase.database().reference()

class DataService {

static let dataService = DataService()

private var _BASE_REF = rootRef
private var _PHOTO_REF = rootRef.child("photos")

var BASE_REF: FIRDatabaseReference {
return _BASE_REF
}

var PHOTO_REF: FIRDatabaseReference {
return _PHOTO_REF
}

var storageRef: FIRStorageReference{
return FIRStorage.storage().reference()
}

var fileUrl: String!



// Share Photo Data

func shareNewPhoto(user: FIRUser, caption: String, data: NSData) {
let filePath = "\(user.uid)/\ (Int(NSDate.timeIntervalSinceReferenceDate))"
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpg"
storageRef.child(filePath).put(data as Data, metadata: metaData) { (metadata, error) in
if let error = error {
print("Error uploading: /\(error.localizedDescription)")

}

// Create a Url for data ( Story Photo)

self.fileUrl = metadata!.downloadURLs![0].absoluteString
if let user = FIRAuth.auth()?.currentUser {
let idPhotoRoom = self.BASE_REF.child("PhotoRooms").childByAutoId()
idPhotoRoom.setValue(["caption": caption, "StoryPhotoUrlFromStorage": self.storageRef.child(metadata!.path!).description, "fileUrl": self.fileUrl])
}
}


// Story Photo (upload and dowload from server)

func fetchDataFromServer(callback:@escaping (StoryPhoto) -> ()) {
DataService.dataService._PHOTO_REF.observe(.childAdded, with: { (snapshot) in
let photo = StoryPhoto(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
callback(photo)

})



// Sign Up

func signUp(username: String, email: String, password: String, data: NSData) {
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}

let changeRequest = user?.profileChangeRequest()
changeRequest?.displayName = username
changeRequest?.commitChanges(completion: { (error) in
if let error = error{
print(error.localizedDescription)
return
}
})

let filePath = "profileimage/\(user!.uid)"
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpeg"

self.storageRef.child(filePath).put(data as Data, metadata: metaData, completion: { (metadata, error) in
if let error = error {
print("\(error.localizedDescription)")
return
}
self .fileUrl = metadata?.downloadURLs![0].absoluteString
let changeRequestPhoto = user!.profileChangeRequest()
changeRequestPhoto.photoURL = NSURL(string: self.fileUrl) as URL?
changeRequestPhoto.commitChanges(completion: { (error) in
if let error = error {
print(error.localizedDescription)
return
}else{
print("profile uptaded")
}
})

let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.Login()

})


})
}

}


}

}


Sign Up class. Here i am getting error like Value of type DataService has no member "Sign up".

// Register Button

@IBAction func RegisterDidTapped(_ sender: AnyObject) {
guard let email = emailTextField.text, let password = passwordTextField.text, let username = usernameTextField.text else {
return
}

var data = NSData()
data = UIImageJPEGRepresentation(profileImage.image!, 0.1)! as NSData

//Signin up


Here i am getting Error: (Value of type DataService has no member "sign Up")

DataService.dataService.signUp(username: username, email: email, password: password, data: data)


}

}

Answer

check correct open and close {}. looks like your signUp function are inside other function.

Comments