我使用2个textfield作为String,String将数据保存到userDeafults但我想知道如何使用循环或函数检索要显示的所有数据
let save = UserDefaults.standard
let heading = headingText.text
let description = desxriptionTexr.text
save.set(description, forKey: heading!)
答案 0 :(得分:9)
要获取 UserDefaults 中的所有键和相应值,您可以使用:
for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
print("\(key) = \(value) \n")
}
在 swift 3 中,您可以使用以下方式进行存储和检索:
UserDefaults.standard.setValue(token, forKey: "user_auth_token")
print("\(UserDefaults.standard.value(forKey: "user_auth_token")!)")
答案 1 :(得分:1)
我认为这不是一个正确的方法。
我建议您将值放入字典并将字典设置为UserDefaults。
let DictKey = "MyKeyForDictionary"
var userDefaults = UserDefaults.standard
// create your dictionary
let dict: [String : Any] = [
"value1": "Test",
"value2": 2
]
// set the dictionary
userDefaults.set(dict, forKey: DictKey)
// get the dictionary
let dictionary = userDefaults.object(forKey: DictKey) as? [String: Any]
// get value from
let value = dictionary?["value2"]
// iterate on all keys
guard let dictionary = dictionary else {
return
}
for (key, val) in dictionary.enumerated() {
}
答案 2 :(得分:0)
您可以在此处将键设置为标题,将值设置为描述。
您可以使用$response = array("error" => FALSE);
$sl_response["error"] = FALSE;
$sl_response["sl_summ"] = array();
$user = $db->getUserByUsernameAndPassword($username, $password);
if ($user != null) {
// user is found
$response["login"]["error"] = FALSE;
$response["login"]["user"]["br_code"] = $user["MEMBER_ID_BRCODE"];
$response["login"]["user"]["mem_id"] = $user["MEMBER_ID"];
$response["login"]["user"]["username"] = $user["USERNAME"];
$response["login"]["user"]["email"] = $user["EMAIL"];
$response["login"]["user"]["created_at"] = $user["REG_DATE"];
json_encode($response, true);
//Displaying json value
echo json_encode($response, true);
// FOR SL SUMM
$user_sldtl = $db->getUserSLsummary($arclass, $loanclass, $accintreceivable, $date, $year, $month, $br_code, $clientid);
If($user_sldtl != null) {
for($i = 0; $i < count($user_sldtl); $i++){
$item = array();
$item["sl_desc"] = $user_sldtl[$i][7];
$item["tr_date"] = $user_sldtl[$i][10];
$item["actual_balance"] = $user_sldtl[$i][14];
$item["available_balance"] = $user_sldtl[$i][14];
$sl_response["sl_summ"][] = $item;
}
$response["accounts"] = $sl_response;
json_encode(response);
echo json_encode($response, true);
}
else {
$sl_response["error"] = TRUE;
$sl_response["error_msg"] = "NO SL Details found!";
echo json_encode($sl_response);
}
}
else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
json_encode($response);
echo json_encode($response);
// echo "<br />" .$username. "<br />";
// echo $password;
}
,UserDefaults.standard.object(forKey:)
,UserDefaults.standard.string(forKey: )
等函数从userDefaults中检索值。
因此,更好地保存2个不同键的标题和描述可能就像
UserDefaults.standard.value(forKey: )
你可以像
那样检索let save = UserDefaults.standard
let heading = headingText.text
let description = desxriptionTexr.text
save.set(description, forKey: "description")
save.set(heading, forKey: "heading")
答案 3 :(得分:0)
如果您有多个字符串,只需将值保存为数组
edit closereq