Objective c 在pdf文件中查找字符串的位置

Objective c 在pdf文件中查找字符串的位置,objective-c,ios,Objective C,Ios,我试图在pdf文件中找到字符串的字节位置 但它并没有给我真正的价值 NSString *string2 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]]; int count =0; int ref=0; int gh=0; // array contains all the line in th epdf NSAr

我试图在pdf文件中找到字符串的字节位置 但它并没有给我真正的价值

NSString *string2 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"test" ofType:@"pdf"]];

    int count =0;
    int ref=0;
    int gh=0;

// array contains all the line in th epdf
 NSArray *array = [string2 componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    for ( NSString *libe in array) {

        ref++;



         count=count+[libe lengthOfBytesUsingEncoding:NSUTF8StringEncoding];

 if ([@"my string" isEqualToString:libe])
        {



            id objet1 = [array objectAtIndex:ref];

            NSString *val1=(NSString*)objet1;


            NSRange range = [val1 rangeOfString:@"mystring" options:NSBackwardsSearch];
total =count+range.location;

}

}

你应该说它给了你什么价值,你期望什么。但是,PDF文件不是原始文本。不能将它们加载到字符串中。使用
NSData
加载文件,并使用
-rangeOfData:options:range:
搜索字节序列(它与字符串不同,但可能与特定编码中的字符串相同)。但我希望查找字符串的位置,而不是搜索字符串,这就是我写的内容。您需要将PDF文件加载到
NSData
中,并使用
-rangeOfData:options:range:
在其中搜索与您在某些特定编码中查找的字符串对应的字节序列。我不知道在二进制PDF文件中可以找到什么编码,但是.NSString*string2=[NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“test”of type:@“PDF”];NSRange range=[string2 rangeOfString:@“Mystring”选项:NSForcedOrderingSearch];NSLog(@“位置估计%u”,范围位置+范围长度);我非常怀疑这会可靠地起作用。您无法从PDF文件可靠地创建
NSString
。PDF文件不是纯文本。