我是StackOverflow的新手,如果我在问题中忘记了一些关键元素,请提前道歉.这是一个很长的,所以忍受我!
迭代JSON-webservice结果:
对于IOS5 iPad应用程序,我从Web服务收集JSON信息,并通过:
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:data options:kNilOptions
error:&error];
Run Code Online (Sandbox Code Playgroud)
我将迭代返回NSDictionary并将返回的对象及其属性存储为自定义类.
自定义类包含多个属性,包括AmountInShoppingCart-value.
我使用这个自定义类来表示我们在商店中销售的商品,因此一个NSMutableArray由所有返回的对象组成并显示给用户.如果是AmountInShoppingCart value > 0,则将对象放入ShoppingCart NSMutableArray(这是一个单例).
检查ShoppingCart中的重复项:
为了确保ShoppingCart它不包含任何重复的条目,我在ShoppingCartController(也借用SO)中有这样的函数:
- (void)checkForDuplicates
{
NSMutableSet *seen = [NSMutableSet set];
NSUInteger i = 0;
NSLog(@"ShoppingCartArray count: %u",[ShoppingCartArray count]);
//Iterate over the Array to check for duplicates, and to sync the list with the
cart.
while (i < [ShoppingCartArray count]) {
id obj = …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,用" - "作为分隔符来显示库存量.
它的构建如下:localStock- wareHouseStock-supplierStock
现在我想更新字符串末尾的supplierStock,但正如您在下面的代码中看到的那样,当原始字符串返回的空间值超过单个空格值(例如20)时,它会出错.
有没有办法删除所有字符,直到最后一个" - "(或删除第二个" - "后面的字符)?
NSMutableString *string1 = [NSMutableString stringWithString: p1.colorStock];
NSLog(@"string1: %@",string1);
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSLog(@"newString: %@",newString);
NSString *colorStock = [NSString stringWithFormat:@"%@-%@",newString,p2.supplierStock];
NSLog(@"colorstock: %@",colorStock);
p1.colorStock = colorStock;
Run Code Online (Sandbox Code Playgroud)
NSLog1
string1: 0-0-0
newString: 0-0
colorstock: 0-0-20
Run Code Online (Sandbox Code Playgroud)
NSLog2
string1: 0-0-20
newString: 0-0-
colorstock: 0-0--20
Run Code Online (Sandbox Code Playgroud)
编辑:感谢Srikar让它工作!
NSString *string1 = [NSString stringWithString: p1.colorStock];
NSLog(@"string1: %@",string1);
NSString *finalString = [string1 stringByReplacingOccurrencesOfString:[[string1 componentsSeparatedByString:@"-"] lastObject] withString:p2.supplierStock.stringValue];
NSLog(@"finalString: %@",finalString);
p1.colorStock = finalString;
Run Code Online (Sandbox Code Playgroud)