Objective c 如何将文本字段中的每个字母分配给Xcode中的数字
我不确定如何将字母表中的每个字母分配给一个数字,例如Objective c 如何将文本字段中的每个字母分配给Xcode中的数字,objective-c,ios,variables,Objective C,Ios,Variables,我不确定如何将字母表中的每个字母分配给一个数字,例如a=1、B=2等。因此,当用户在文本字段中键入例如姓名时,所有字母的数值相加并作为总数给出 这最终将允许我显示基于字母总数的特定输出 干杯,利奥创建一个包含所有字母的枚举。然后一次循环输入字符串一个字符,并将值相加 enum { A=1, B=2, C=3, // etc... }; typedef int AlphabetIntCodes; 然后使用以下方法循环: NSMutableString *textString
a=1、B=2
等。因此,当用户在文本字段中键入例如姓名时,所有字母的数值相加并作为总数给出
这最终将允许我显示基于字母总数的特定输出
干杯,利奥创建一个包含所有字母的枚举。然后一次循环输入字符串一个字符,并将值相加
enum {
A=1,
B=2,
C=3,
// etc...
};
typedef int AlphabetIntCodes;
然后使用以下方法循环:
NSMutableString *textString = [[NSMutableString alloc] initWithString:txtText.text];
NSString *string = [NSMutableString stringWithFormat:@"%@",textString];
int stringLength = textString.length;
for (int i=0; i<stringLength; i++) {
string = [string characterAtIndex:i];
// Find the int value from the enum, and add it to a variable...
}
NSMutableString*textString=[[NSMutableString alloc]initWithString:txtText.text];
NSString*string=[NSMutableString stringWithFormat:@“%@”,textString];
int stringLength=textString.length;
对于(inti=0;i我想到的最简单的解决方案,但它只会在根据字母顺序分配增量值的情况下起作用
// Make every letter uppercase to compare on the same scale of unicode values
NSString * str = [yourTextField.text uppercaseString];
NSUInteger total = 0;
for (int i = 0 ; i < str.length ; ++i ) {
// 'A' unicode value is 65, so by substracting 64 you'll get 1 for A, 2 for B, 3 for C...
total += [str characterAtIndex:i] - 64;
}
NSLog(@"Total is %d", total);
//使每个字母大写,以便在相同的unicode值比例上进行比较
NSString*str=[yourTextField.text uppercaseString];
整数合计=0;
对于(int i=0;i
你仍然应该确保你没有得到任何符号,但这将汇总字符串中的所有字母。如果这是家庭作业,请在你的问题中添加家庭作业
标记。