我正在尝试使用Firebase进行“登录/注册”屏幕。我创建了一个用于将数据发送到Firebase的快捷类,并将其称为“ DataService”类。我也有登录和注册课程。在“ DataService”类中,我无法创建多个函数,当我尝试创建注册函数时,出现类似“ DataService类型的值”没有成员“ Sign up”的错误。但是其他功能可以正常工作。在此类(DataService)中,我只能有一个函数。
DATASERVICE类
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()
})
})
}
}
}
}
注册课程。在这里,我遇到类似DataService类型的值没有成员“注册”的错误。
// 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)
}
}
最佳答案
您的问题是将功能放在其他功能中。在这种情况下,请勿这样做。需要从其他代码调用的函数应该是该类的顶级函数,而不是嵌入在其他函数中。
并且,无关的,请使用正确的标准命名约定。方法和变量名应以小写字母开头。类名以大写字母开头。
关于ios - Xcode 8,Swift3。DataService类型的值没有成员“Sign up”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40578421/