本周末我完成了我的第一个应用程序(感谢Stack Overflow用户!),我目前正在优化代码。我已经修复了大部分重复和不安全的做法,但这个让我陷入困境。
当用户将其添加到他的钱包时,我将如何将给定加密货币所需的所有信息保存到Core Data中:
if addedCrypto == "Augur REP" {
if CoreDataHandler.saveObject(name: "Augur", code: "augur", symbol: "REP", placeholder: "REP Amount", amount: "0.00000000", amountValue: "0.0") {
for _ in CoreDataHandler.fetchObject()! {
}
}
}
这对于一个加密来说非常方便,但我的应用程序支持其中的25个。目前,在我的代码中,上述行重复了24次,每次不同的加密一次。
我考虑使用字典,我可以将Augur REP
保存为key
,然后将(name: "Augur", code: "augur", ...")
保存为value
,但我无法弄清楚如何正确地将saveObject(...)
保存为class func saveObject(name:String, code:String, symbol:String, placeholder:String, amount:String, amountValue:String) -> Bool {
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "CryptosMO", in: context)
let managedObject = NSManagedObject(entity: entity!, insertInto: context)
managedObject.setValue(name, forKey: "name")
managedObject.setValue(code, forKey: "code")
managedObject.setValue(symbol, forKey: "symbol")
managedObject.setValue(placeholder, forKey: "placeholder")
managedObject.setValue(amount, forKey: "amount")
managedObject.setValue(amountValue, forKey: "amountValue")
do {
try context.save()
return true
} catch {
return false
}
}
做它..
这里可能有什么解决方案?
编辑:这是<?php
require_once('vendor/autoload.php');
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<style type="text/css">
@media(print)
{
h1
{
font-size: 16px;
box-shadow: inset 0px 0px 0px 1px rgba(0,0,0,1);
}
table
{
width: 100%;
font-size: 13px;
border: none;
}
td
{
border: 1px black solid;
box-shadow: inset 0px 0px 0px 1px rgba(0,0,0,1);
}
}
</style>
</head>
<body>
<h1>
Headline
</h1>
<table>
<tr>
<td>blabla</td>
<td>blabla</td>
<td>blabla</td>
</tr>
</table>
</body>
</html>
<?php
$content = ob_get_clean();
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetHTMLFooter('<div class="footer"><span class="pagenum">Seite: {PAGENO} / {nbpg}</span></div>');
$mpdf->WriteHTML($content);
$mpdf->Output();
方法:
curl -u API_Key:Secret_Key 'https://amplitude.com/api/2/funnels?e={"event_type":"Any%20Active%20Event"}&e={"event_type":"Purchase%20Confirmation%20Page%20-%20Page%20View%20-%20W"\}&start=20180205&end=20180211&n=active&mode=unordered&cs=60'
答案 0 :(得分:2)
我认为你应该为存储这些信息的元组类型创建一个类型别名:
typealias CryptosMOInfo = (name:String, code:String, symbol:String, placeholder:String, amount:String, amountValue:String)
然后你可以创建一个这样的字典:
let cryptosDictionary: [String, CryptosMOInfo] = [
"Augur REP": (name: "Augur", code: "augur", symbol: "REP", placeholder: "REP Amount", amount: "0.00000000", amountValue: "0.0"),
// ...
]
saveObject
方法的签名可以更改为:
static func saveOject(cryptosInfo: CryptosMOInfo) -> Bool
请记得访问cryptosInfo
:
managedObject.setValue(cryptosInfo.name, forKey: "name")
managedObject.setValue(cryptosInfo.code, forKey: "code")
managedObject.setValue(cryptosInfo.symbol, forKey: "symbol")
managedObject.setValue(cryptosInfo.placeholder, forKey: "placeholder")
managedObject.setValue(cryptosInfo.amount, forKey: "amount")
managedObject.setValue(cryptosInfo.amountValue, forKey: "amountValue")
如果您不喜欢类型别名,也可以将其更改为struct
。
答案 1 :(得分:0)
为什么不直接使用托管对象子类? 添加新加密时创建新实例,然后一次保存所有已更新/更新的对象。
if addedCrypto == "Augur REP" {
let crypto = CryptosMO(context: CoreDataHandler.getContext())
crypto.name = "Augur"
crypto.code = "augur"
// and so on
}
由于此代码似乎相同,因此您可以为支持的每个加密创建工厂方法,以使代码更易于阅读。
if addedCrypto == "Augur REP" {
_ = CryptoFactory.createAugur()
}
class CryptoFacory {
static func CreateAugur() -> CryptoMO {
return create(name: "Augur", code: "augur",...
}
//... other crypto factory methods
private static create(name: String, code: String,...) -> CryptoMO {
let crypto = CryptosMO(context: CoreDataHandler.getContext())
crypto.name = name
crypto.code = code
//...
return crypto
}
然后,CoreDataHandler中的save方法不需要任何参数,因为crypto实例已经在托管对象上下文中。所以它只是
func save() -> Bool {
let context = getContext()
do {
try context.save()
return true
} catch {
return false
}
}