读取“CSV”文件 - 例外

时间:2014-02-12 11:28:29

标签: ios objective-c csv

我有一个问题,希望你能帮助我。

问题:2.1.17之后如何解决异常和读取CSV。我知道索引2中的数组中没有元素。如何从索引2处的数组中获取我想要的CSV文本?

这是我的代码:

    NSString *filepathName = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"csv"];
    NSString *contentName =  [NSString stringWithContentsOfFile:filepathName  encoding:NSWindowsCP1251StringEncoding error:nil];
    NSMutableArray *pointStringsName = [[NSMutableArray alloc] initWithArray:[contentName componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

    for(int idx = 0; idx < [pointStringsName count]; idx++)
    {
        NSString *currentPointStringName = [pointStringsName objectAtIndex:idx];
        if ([currentPointStringName length] == 0) {
            continue;
        }

        NSArray *arrayName = [currentPointStringName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@";"]];

        NSLog(@"1: %@; 2: %@; 3: %@;", [arrayName objectAtIndex:0], [arrayName objectAtIndex:1], [arrayName objectAtIndex:2]);
    }

我的csv文件:

2.1.17.;Íîòàðèóñ;Notary
2.2.1.;"Óïðàâëåíèå îñóùåñòâëÿåò ôóíêöèè ïî êîíòðîëþ è íàäçîðó â ñôåðå îáåñïå÷åíèÿ ñàíèòàðíî-ýïèäåìèîëîãè÷åñêîãî áëàãîïîëó÷èÿ íàñåëåíèÿ, çàùèòû ïðàâ ïîòðåáèòåëåé è ïîòðåáèòåëüñêîãî ðûíêà. Îñíîâíûìè çàäà÷àìè óïðàâëåíèÿ ÿâëÿþòñÿ:
- îðãàíèçàöèÿ è îñóùåñòâëåíèå ãîñóäàðñòâåííîãî íàäçîðà è êîíòðîëÿ çà èñïîëíåíèåì îáÿçàòåëüíûõ òðåáîâàíèé çàêîíîäàòåëüñòâà Ðîññèéñêîé Ôåäåðàöèè â îáëàñòè îáåñïå÷åíèÿ ñàíèòàðíî-ýïèäåìèîëîãè÷åñêîãî áëàãîïîëó÷èÿ íàñåëåíèÿ, çàùèòû ïðàâ ïîòðåáèòåëåé, è â îáëàñòè ïîòðåáèòåëüñêîãî ðûíêà;
- ïðåäóïðåæäåíèå âðåäíîãî âîçäåéñòâèÿ íà ÷åëîâåêà ôàêòîðîâ ñðåäû îáèòàíèÿ;
- ïðîôèëàêòèêà èíôåêöèîííûõ è ìàññîâûõ íåèíôåêöèîííûõ çàáîëåâàíèé (îòðàâëåíèé) íàñåëåíèÿ.";"The Departmant implements control and supervision in the field of sanitary and epidemiological welfare of the population , protection of consumer rights and consumer markets. Main tasks of the Departmant are:
- to organize and implement state supervision and control over the execution of major requirements of the Russian Frderation legislation in the field of sanitary and epidemiological welfare of the population, consumer protection, and consumer market ;
- to prevent harmful effects of  environmental factors on  humans ;
- to prevent infective desiases and large-scale  noncommunicable disease dissemination among population"
2.2.2.;"Óïðàâëåíèå ïðîâîäèò ãîñóäàðñòâåííóþ ïîëèòèêó â îáëàñòè îðãàíèçàöèè è îñóùåñòâëåíèÿ ãîñóäàðñòâåííîãî ïîæàðíîãî íàäçîðà. 
Îñíîâíûìè çàäà÷àìè Óïðàâëåíèÿ ÿâëÿþòñÿ: 
- îñóùåñòâëåíèå ìåð ïî çàùèòå æèçíè è çäîðîâüÿ ãðàæäàí, èõ èìóùåñòâà, ãîñóäàðñòâåííîãî è ìóíèöèïàëüíîãî èìóùåñòâà, à òàêæå èìóùåñòâà îðãàíèçàöèé îò ïîæàðîâ è îãðàíè÷åíèþ èõ ïîñëåäñòâèé;
- îñóùåñòâëåíèå â óñòàíîâëåííîì ïîðÿäêå ñáîðà è îáðàáîòêè èíôîðìàöèè â îáëàñòè îáåñïå÷åíèÿ ïîæàðíîé áåçîïàñíîñòè, à òàêæå îáìåíà ýòîé èíôîðìàöèåé.";" The Department executes state policy in the field of the state fire inspection organization and implementation.
The main objectives of the Department are :
- to implement measures to protect the life and health of citizens and their property, state and municipal property  and organization property from fires and to limit their consequences ;
- to implement collecting and processing of information in the field of fire safety, according to the established procedure, and implement information exchange."

我的NSLog:

1: 2.1.17.; 2: Нотариус; 3: Notary;
2014-02-12 15:14:22.846 CSVSQLITE[1377:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

非常感谢先进。

NSLog arrayName:

2014-02-12 15:42:11.427 CSVSQLITE[1473:70b] (
    "2.1.17.",
    "\U041d\U043e\U0442\U0430\U0440\U0438\U0443\U0441",
    Notary
)
2014-02-12 15:42:11.427 CSVSQLITE[1473:70b] (
    "2.2.1.",
    "\"\U0423\U043f\U0440\U0430\U0432\U043b\U0435\U043d\U0438\U0435 \U043e\U0441\U0443\U0449\U0435\U0441\U0442\U0432\U043b\U044f\U0435\U0442 \U0444\U0443\U043d\U043a\U0446\U0438\U0438 \U043f\U043e \U043a\U043e\U043d\U0442\U0440\U043e\U043b\U044e \U0438 \U043d\U0430\U0434\U0437\U043e\U0440\U0443 \U0432 \U0441\U0444\U0435\U0440\U0435 \U043e\U0431\U0435\U0441\U043f\U0435\U0447\U0435\U043d\U0438\U044f \U0441\U0430\U043d\U0438\U0442\U0430\U0440\U043d\U043e-\U044d\U043f\U0438\U0434\U0435\U043c\U0438\U043e\U043b\U043e\U0433\U0438\U0447\U0435\U0441\U043a\U043e\U0433\U043e \U0431\U043b\U0430\U0433\U043e\U043f\U043e\U043b\U0443\U0447\U0438\U044f \U043d\U0430\U0441\U0435\U043b\U0435\U043d\U0438\U044f, \U0437\U0430\U0449\U0438\U0442\U044b \U043f\U0440\U0430\U0432 \U043f\U043e\U0442\U0440\U0435\U0431\U0438\U0442\U0435\U043b\U0435\U0439 \U0438 \U043f\U043e\U0442\U0440\U0435\U0431\U0438\U0442\U0435\U043b\U044c\U0441\U043a\U043e\U0433\U043e \U0440\U044b\U043d\U043a\U0430. \U041e\U0441\U043d\U043e\U0432\U043d\U044b\U043c\U0438 \U0437\U0430\U0434\U0430\U0447\U0430\U043c\U0438 \U0443\U043f\U0440\U0430\U0432\U043b\U0435\U043d\U0438\U044f \U044f\U0432\U043b\U044f\U044e\U0442\U0441\U044f:"
)
2014-02-12 15:42:11.428 CSVSQLITE[1473:70b] (
    "- \U043e\U0440\U0433\U0430\U043d\U0438\U0437\U0430\U0446\U0438\U044f \U0438 \U043e\U0441\U0443\U0449\U0435\U0441\U0442\U0432\U043b\U0435\U043d\U0438\U0435 \U0433\U043e\U0441\U0443\U0434\U0430\U0440\U0441\U0442\U0432\U0435\U043d\U043d\U043e\U0433\U043e \U043d\U0430\U0434\U0437\U043e\U0440\U0430 \U0438 \U043a\U043e\U043d\U0442\U0440\U043e\U043b\U044f \U0437\U0430 \U0438\U0441\U043f\U043e\U043b\U043d\U0435\U043d\U0438\U0435\U043c \U043e\U0431\U044f\U0437\U0430\U0442\U0435\U043b\U044c\U043d\U044b\U0445 \U0442\U0440\U0435\U0431\U043e\U0432\U0430\U043d\U0438\U0439 \U0437\U0430\U043a\U043e\U043d\U043e\U0434\U0430\U0442\U0435\U043b\U044c\U0441\U0442\U0432\U0430 \U0420\U043e\U0441\U0441\U0438\U0439\U0441\U043a\U043e\U0439 \U0424\U0435\U0434\U0435\U0440\U0430\U0446\U0438\U0438 \U0432 \U043e\U0431\U043b\U0430\U0441\U0442\U0438 \U043e\U0431\U0435\U0441\U043f\U0435\U0447\U0435\U043d\U0438\U044f \U0441\U0430\U043d\U0438\U0442\U0430\U0440\U043d\U043e-\U044d\U043f\U0438\U0434\U0435\U043c\U0438\U043e\U043b\U043e\U0433\U0438\U0447\U0435\U0441\U043a\U043e\U0433\U043e \U0431\U043b\U0430\U0433\U043e\U043f\U043e\U043b\U0443\U0447\U0438\U044f \U043d\U0430\U0441\U0435\U043b\U0435\U043d\U0438\U044f, \U0437\U0430\U0449\U0438\U0442\U044b \U043f\U0440\U0430\U0432 \U043f\U043e\U0442\U0440\U0435\U0431\U0438\U0442\U0435\U043b\U0435\U0439, \U0438 \U0432 \U043e\U0431\U043b\U0430\U0441\U0442\U0438 \U043f\U043e\U0442\U0440\U0435\U0431\U0438\U0442\U0435\U043b\U044c\U0441\U043a\U043e\U0433\U043e \U0440\U044b\U043d\U043a\U0430",
    ""
)
2014-02-12 15:42:11.429 CSVSQLITE[1473:70b] (
    "- \U043f\U0440\U0435\U0434\U0443\U043f\U0440\U0435\U0436\U0434\U0435\U043d\U0438\U0435 \U0432\U0440\U0435\U0434\U043d\U043e\U0433\U043e \U0432\U043e\U0437\U0434\U0435\U0439\U0441\U0442\U0432\U0438\U044f \U043d\U0430 \U0447\U0435\U043b\U043e\U0432\U0435\U043a\U0430 \U0444\U0430\U043a\U0442\U043e\U0440\U043e\U0432 \U0441\U0440\U0435\U0434\U044b \U043e\U0431\U0438\U0442\U0430\U043d\U0438\U044f",
    ""
)
2014-02-12 15:42:11.429 CSVSQLITE[1473:70b] (
    "- \U043f\U0440\U043e\U0444\U0438\U043b\U0430\U043a\U0442\U0438\U043a\U0430 \U0438\U043d\U0444\U0435\U043a\U0446\U0438\U043e\U043d\U043d\U044b\U0445 \U0438 \U043c\U0430\U0441\U0441\U043e\U0432\U044b\U0445 \U043d\U0435\U0438\U043d\U0444\U0435\U043a\U0446\U0438\U043e\U043d\U043d\U044b\U0445 \U0437\U0430\U0431\U043e\U043b\U0435\U0432\U0430\U043d\U0438\U0439 (\U043e\U0442\U0440\U0430\U0432\U043b\U0435\U043d\U0438\U0439) \U043d\U0430\U0441\U0435\U043b\U0435\U043d\U0438\U044f.\"",
    "\"The Departmant implements control and supervision in the field of sanitary and epidemiological welfare of the population , protection of consumer rights and consumer markets. Main tasks of the Departmant are:"
)
2014-02-12 15:42:11.429 CSVSQLITE[1473:70b] (
    "- to organize and implement state supervision and control over the execution of major requirements of the Russian Frderation legislation in the field of sanitary and epidemiological welfare of the population, consumer protection, and consumer market ",
    ""
)
2014-02-12 15:42:11.430 CSVSQLITE[1473:70b] (
    "- to prevent harmful effects of  environmental factors on  humans ",
    ""
)
2014-02-12 15:42:11.430 CSVSQLITE[1473:70b] (
    "- to prevent infective desiases and large-scale  noncommunicable disease dissemination among population\""
)

0 个答案:

没有答案
相关问题