我的公司有一個直接與Salesforce連接的iOS應用程序,但有一個主要缺點。當用戶嘗試上傳非法字符時,應用程序掛起,返回錯誤值或導致其他不穩定行爲。上傳前在字典中轉義字符 - 目標C
我在神祕的interwebs(感謝,MailDrop)中發現了一個非常方便的代碼片段,它可以過濾一個字符串並用正斜槓轉義這些非法字符。然後它返回字符串中的值,準備插入/查詢。
但是,我真正遇到的一件事是讓我們的應用程序能夠正常工作。我們的數據存儲在字典中,並以各種方式(查詢服務器或手動輸入)以及在應用程序生命週期中的不同時間寫入。我不太清楚如何用最有效的方法調用這個漂亮的'escapeSosl'。每次我寫入Core Data時,是否有辦法逃避這些角色?我最害怕的是通過應用程序並打電話數百次。
這裏是逃脫方法:
- (NSString *)escapeSosl:(NSString *)src {
// from docs, these are all reserved
NSArray *reserved = [NSArray arrayWithObjects:@"\\", @"&", @"|", @"!", @"{", @"}", @"[", @"]", @"^", @"~", @"*:", @":", @"'" ,@"\"", @"+", @"-", nil];
NSMutableString *s = [NSMutableString stringWithString:src];
NSString *r;
NSEnumerator *e = [reserved objectEnumerator];
while (r = [e nextObject])
[s replaceOccurrencesOfString:r withString:[NSString stringWithFormat:@"\\%@", r] options:NSLiteralSearch range:NSMakeRange(0, [s length])];
return s;
}
我發現複雜的是這些escapings的時機,以及如何應用到各種情況。例如,應用程序使用手動輸入的位置或通過反向地理編碼查找的位置查詢Salesforce的帳戶。下面是該應用從Salesforce的服務器請求數據的例子:
-(void)checkServerForLeadMatchingAddressOnAccount:(SFAccount *)account {
currentAccount = account;
//Parse the account location data, removing the last word in the `street` object
NSString *street = account.shippingStreetAddress;
NSMutableArray *streetArray = [[street componentsSeparatedByString:@" "] mutableCopy];
[streetArray removeLastObject];
street = [streetArray componentsJoinedByString:@" "];
street = [street stringByAppendingString:@"_%"];
NSString *city = account.shippingCity;
NSString *state = account.shippingState;
NSString *zip = account.shippingZIP;
//Check for a matching lead on the server. User the callback methods to proceed (either create new data on the server or download to the device
NSLog(@"is connected: %i", self.isConnectedToServer);
if (self.isConnectedToServer){
requestCheckForLead = [[SFRestAPI sharedInstance] requestForQuery:[NSString stringWithFormat:@"SELECT ID, Company, Name, Street, City, State, PostalCode, Phone FROM Lead WHERE PostalCode = '%@' AND Street LIKE '%@' AND City = '%@' AND State = '%@'", zip, street, city, state]];
[[SFRestAPI sharedInstance] send:requestCheckForLead delegate:self];
openTasks++;
} else {
NSLog(@"I can't connect");
[self.serverDelegate serverObjectManager:self communicationSuccessful:NO withResponse:@"Could not connect to the server."];
}
另一個領域,我可能轉義字符是正確的開始上傳到服務器之前。有幾件事情會上傳,包括賬戶,賬戶的聯繫人(1:1 rel。),賬戶的機會(1:1 rel。)和機會當前服務級別(1:many rel)。
下面是一個與帳戶關聯的聯繫人示例 - 它既使用字符串引用,也使用字典引用,但具有一些用於解析lastName的firstName的附加邏輯。我想以一種不需要擔心這些操作時機的方式來逃避數據。
NSMutableDictionary *contactDict = [NSMutableDictionary new];
if (myAccount.contact.email) {
[contactDict setObject:myAccount.contact.email forKey:@"Email"];
} else {
[contactDict setObject:@"" forKey:@"Email"];
}
if (myAccount.contact.name) {
//Split name in two
NSArray *nameArray = [myAccount.contact.name componentsSeparatedByString:@" "];
NSString *firstName = [nameArray objectAtIndex:0];
NSString *lastName = [nameArray lastObject];
[contactDict setObject:firstName forKey:@"firstName"];
[contactDict setObject:lastName forKey:@"lastName"];
} else {
[contactDict setObject:@"" forKey:@"firstName"];
[contactDict setObject:@"" forKey:@"lastName"];
}
if (myAccount.contact.phone){
[contactDict setObject:myAccount.contact.phone forKey:@"Phone"];
} else {
[contactDict setObject:@"" forKey:@"Phone"];
}
updateContactRequest = [[SFRestAPI sharedInstance] requestForUpdateWithObjectType:@"Contact" objectId:myAccount.createdContactID fields:contactDict];
[[SFRestAPI sharedInstance] send:updateContactRequest delegate:self];
因此,這似乎是有點大的問題,但沒有任何人有一些指點,我怎麼能在這些各種字典轉義字符,使用類似於「escapeSosl」的代碼,我上面包含的東西,無論何時寫入核心數據?
Superfell,感謝您的正確語法。我認爲在插入SOQL之前,我必須在別的地方調用街道,城市,州,郵編參數。我很感激! – CDD 2013-03-14 14:38:33