英文:
How do I concatenate two strings into an encoded base64 string in swift?
问题
为了验证用户登录到我们的数据库,我需要捕获他们的用户名和密码,然后将这两个字符串连接起来,然后转换为base64。我是Swift编程的新手,尝试了一些我在谷歌上找到的方法,但都没有成功。
以下是我迄今为止的代码,它不起作用:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var userName: UITextField!
@IBOutlet weak var password: UITextField!
@IBAction func loginButton(_ sender: Any) {
let login = (userName.text != nil) && (password.text != nil)
let encoded = login.data(using: .utf8)?.base64EncodedString()
}
}
无论我采用什么方法,我都会收到两个错误:
无法推断引用成员'utf8'的上下文基数
以及
类型'Bool'的值没有成员'data'
希望有所帮助。
英文:
In order to authenticate a user into our database, I need to capture their login and password, concatenate both strings and then convert it to base64. I am new to swift programming and have tried several things I googled to no avail.
Here is what I have so far, that doesn't work:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var userName: UITextField!
@IBOutlet weak var password: UITextField!
@IBAction func loginButton(_ sender: Any) {
let login = (userName.text != nil) && (password.text != nil)
let encoded = login.data(using: .utf8)?.base64EncodedString()
}
}
I keep getting two errors regardless of my approach:
Cannot infer contextual base in reference to member 'utf8'
and
Value of type 'Bool' has no member 'data'
Any help is appreciated.
答案1
得分: 2
It looks like you have a few misunderstandings in your code that are causing these issues.
首先,这行代码:
let login = (userName.text != nil) && (password.text != nil)
正在检查userName.text和password.text是否不为nil,并返回一个布尔值(true或false)。
然后,通过这行代码:
let encoded = login.data(using: .utf8)?.base64EncodedString()
您试图将一个布尔值转换为Data并将其编码为base64字符串。然而,data(using:)方法和base64EncodedString()函数期望一个String,而不是布尔值。
相反,您应该检查userName.text和password.text是否不为nil,如果它们不是nil,那么将它们连接起来并将其转换为base64字符串。以下是您如何可以更正您的代码:
@IBAction func loginButton(_ sender: Any) {
guard let login = userName.text, let pwd = password.text else {
print("Username or password is missing")
return
}
let combined = login + pwd
let data = Data(combined.utf8)
let encoded = data.base64EncodedString()
print(encoded)
}
这是您可以更正代码的方式。
英文:
It looks like you have a few misunderstandings in your code that are causing these issues.
Firstly, this line of code:
let login = (userName.text != nil) && (password.text != nil)
is checking if userName.text and password.text are not nil, returning a boolean value (true or false).
Then, with this line:
let encoded = login.data(using: .utf8)?.base64EncodedString()
you're trying to convert a Bool to Data and encode it as a base64 string. However, the data(using:) method and base64EncodedString() function expect a String, not a Bool.
Instead, you should be checking if userName.text and password.text are not nil, and if they're not, concatenate them and convert that to a base64 string. Here's how you can correct your code:
@IBAction func loginButton(_ sender: Any) {
guard let login = userName.text, let pwd = password.text else {
print("Username or password is missing")
return
}
let combined = login + pwd
let data = Data(combined.utf8)
let encoded = data.base64EncodedString()
print(encoded)
}
答案2
得分: 0
你需要的是这个:
let encoded =
[userName, password]
.compactMap(\.text) // 如果不是 nil,则提取文本
.filter { !$0.isEmpty } // 过滤掉空字符串
.joined(separator: " ") // 使用空格连接它们(你可以使用任何分隔符,或者只传入空字符串)
.data(using: .utf8)? // 转换为数据
.base64EncodedString() // 转换为 base64
并且完全没有强制解包!
英文:
What you need is this:
let encoded =
[userName, password]
.compactMap(\.text) // Extract the text if there is not nil
.filter { !$0.isEmpty } // filter out empty strings
.joined(separator: " ") // concatenate them using a space (you can use any separator you like or just pass in an empty string)
.data(using: .utf8)? // make it data
.base64EncodedString() // make it base 64
And no force unwrap at all!
答案3
得分: 0
以下是已翻译的内容:
您可能正在查看类似以下内容的代码:
@IBAction func loginButton(_ sender: Any) {
guard let name = userName?.text else { return } // 没有用户名
guard let password = password?.text else { return } // 没有密码
let combinedText = name + password
// let combinedText = [name, password].joined(separator: "&") 如果需要两个字符串之间有分隔符,请使用这个
guard let encoded = combinedText.data(using: .utf8) else { return } // 由于某种原因无法编码为UTF8
let base64Encoded = encoded.base64EncodedString()
}
要理解您的错误之处
1. 编写类似于 `userName.text != nil` 的内容意味着“用户名的文本是否为空?”这是一个布尔值,可以是YES/NO或`true`/`false`,即它要么为空,要么不为空。
2. 然后 `let login = (userName.text != nil) && (password.text != nil)` 不会合并任何字符串。它基本上是在问“用户名和密码都不为空吗?”而且再次,它们两者一起要么都不为空,要么至少其中一个为空。因此,它会计算为另一个布尔值,可以是YES/NO或`true`/`false`。
3. 然后,您尝试将其编码为Base64编码的UTF8字符串数据。
从我提供的代码中,所有的安全检查都已添加,并且由于数据不正确,代码可以什么都不做而退出。但如果您希望以不安全的方式进行操作(您的应用程序可能会崩溃),您可以使用一行代码来执行:
let base64Encoded = (userName.text! + password.text!).data(using: .utf8)!.base64EncodedString()
英文:
You might be looking at something like the following:
@IBAction func loginButton(_ sender: Any) {
guard let name = userName?.text else { return } // No user name
guard let password = password?.text else { return } // No password
let combinedText = name + password
// let combinedText = [name, password].joined(separator: "&") Use this if you need a separator between the two strings
guard let encoded = combinedText.data(using: .utf8) else { return } // Could not encode to UTF8 for some reason
let base64Encoded = encoded.base64EncodedString()
}
To understand where you got wrong
- Writing something like
userName.text != nil
means "Is text of username null?" and is a boolean value as in YES/NO ortrue
/false
as in either it is null or it isn't. - Then
let login = (userName.text != nil) && (password.text != nil)
does not combine any string. It basically say "Are both, username and password, non-null?". And again together they both either are non-null or at least one of them is null. So it evaluates to another boolean value as in YES/NO ortrue
/false
. - You then try to encode that as base64 encoded UTF8 string data
From the code I provided all the safety checks are added and code can exit as doing nothing due to incorrect data. But if you wish to do it unsafely (your app my crash) you can do a one-liner:
let base64Encoded = (userName.text! + password.text!).data(using: .utf8)!.base64EncodedString()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论