将NSString转换为For循环内的字符数组 [英] Converting NSString to array of chars inside For Loop

查看:228
本文介绍了将NSString转换为For循环内的字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用iOS项目中的现有代码来将数组中的单词列表(例如,使番茄变成毫无用处,或者堆积成一堆)。代码似乎工作,如果我自己运行它,但我试图将其集成到我现有的应用程序。

每个字我希望它alphabetize存储作为一个数组内的NSString。这个问题似乎是代码把这个词作为一个字符数组,我不能让我的NSString成为这种格式。



如果我使用 string = [currentWord UTFString] ,我得到错误数组类型char [128]不可分配,如果我尝试在循环中创建字符数组( const char * string = [curentWord UTF8String] )我得到与有关的警告类型为const的char初始化char抛出限定符。不太清楚我可以绕过它 - 任何提示?下面的方法,我会照顾存储的alphabetized版本以后。

   - (void)alphabetizeWord {
char char [128],temp;
int n,i,j;

(NSString * currentWord in wordsList){
n = [currentWord length]; (j = i + 1; j if(string [i])的
对于(i = 0; i< n-1; > string [j]){
temp = string [i];
string [i] = string [j];
string [j] = temp;


$ b NSLog(@单词%@按字母顺序排列是%s,currentWord,string);


$ / code $ / pre

解决方案

应该工作:

   - (void)alphabetizeWord {
char str [128];
$ b $ for(NSString * currentWord in wordList)
{
int wordLength = [currentWord length];
for(int i = 0; i {
str [i] = [currentWord characterAtIndex:i];
}
//添加终止字符
str [wordLength] = 0;
// Add your word
}
}

编辑:对不起,起初并不完全了解。要检查一下。


I'm trying to use an existing piece of code in an iOS project to alphabetize a list of words in an array (for instance, to make tomato into amoott, or stack into ackst). The code seems to work if I run it on its own, but I'm trying to integrate it into my existing app.

Each word I want it to alphabetize is stored as an NSString inside an array. The issue seems to be that the code takes the word as an array of chars, and I can't get my NSStrings into that format.

If I use string = [currentWord UTFString], I get an error of Array type char[128] is not assignable, and if I try to create the char array inside the loop (const char *string = [curentWord UTF8String]) I get warnings relating to Initializing char with type const char discards qualifiers. Not quite sure how I can get around it – any tips? The method is below, I'll take care of storing the alphabetized versions later.

- (void) alphabetizeWord {
    char string[128], temp;
    int n, i, j;

    for (NSString* currentWord in wordsList) {
        n = [currentWord length];
        for (i = 0; i < n-1; i++) {
            for (j = i+1; j < n; j++) {
                if (string[i] > string[j]) {
                    temp = string[i];
                    string[i] = string[j];
                    string[j] = temp;
                }
            }
        }
        NSLog(@"The word %@ in alphabetical order is %s", currentWord, string);
    }
}

解决方案

This should work :

- (void)alphabetizeWord {
  char str[128];

  for (NSString *currentWord in wordList)
  {
    int wordLength = [currentWord length];
    for (int i = 0; i < wordLength; i++)
    {
      str[i] = [currentWord characterAtIndex:i];
    }
    // Adding the termination char
    str[wordLength] = 0;
    // Add your word
  }
}

EDIT : Sorry, didn't fully understand at first. Gonna check this out.

这篇关于将NSString转换为For循环内的字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆