2

次のコードでは、文字列の代わりに変数を割り当てたいと思います。まず、ディレクトリのコンマ区切りリストから配列を作成しています。次に、配列内の各ディレクトリについて、存在しない場合は作成します。

@ "templates / standard1 / css"ではなく、配列からディレクトリを割り当てるにはどうすればよいですか。sに置き換えようとしましたが、うまくいきませんでした。をNSlogできますが、ディレクトリが作成されていないようです。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *theString = @"templates, templates/standard1, templates/standard1/css, themes, themes/plain, themes/plain/384";
NSArray *items = [theString componentsSeparatedByString:@","];
for (NSString *s in items) {
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"templates/standard1/css"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        // Directory does not exist so create it
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:nil];
    }

}

どんな助けもいただければ幸いです:)

4

1 に答える 1

3

スペースがあなたのディレクトリを悩ませていると思います。各ディレクトリは「、」で区切られていますが、「、」(スペースなし)を検索しています。文字列を使用するには、スペースを削除するtheStringか、区切り文字として「、」を使用できます。

したがって、どちらかを変更します

NSString *theString = @"templates, templates/standard1, templates/standard1/css, themes, themes/plain, themes/plain/384";

これに

NSString *theString = @"templates,templates/standard1,templates/standard1/css,themes,themes/plain,themes/plain/384";

またはこれを変更します:

NSArray *items = [theString componentsSeparatedByString:@","];

これに:

NSArray *items = [theString componentsSeparatedByString:@", "];

しかし、両方ではありません!!!

于 2012-06-14T18:09:30.850 に答える