0

因此,在我当前的项目中,我下载了一个包含大量数据的大型文本文件。现在我必须搜索该文件并检查它是否包含用户输入的数字。在objective-c中,这看起来像这样:

        NSString *securityCodeData = [[NSUserDefaults standardUserDefaults] stringForKey:@"securityCodeData"];

        NSString *trackTraceFileUrlName = [NSString stringWithFormat:@"http://plex.janssen1877.com/app/life/%@",securityCodeData];
        NSData *tmpData = [NSData dataWithContentsOfURL:[NSURL URLWithString:trackTraceFileUrlName] ];



        //convert data to string
        NSString *tmpString = [[NSString alloc] initWithData:tmpData encoding:NSUTF8StringEncoding];
        NSArray *piecesArray = [tmpString componentsSeparatedByString:@"\n"];
        NSMutableArray *trackTraceContent = [NSMutableArray arrayWithArray:piecesArray];
        NSLog(@"RAW:%@",trackTraceContent);

        /*Filtering array for referance*/
        NSPredicate *findStringWithReference = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",cRText.text];
        NSArray *trackTraceContentFiltered = [trackTraceContent filteredArrayUsingPredicate:findStringWithReference];
        NSLog(@"Filtered:%@",trackTraceContentFiltered);


    if ([trackTraceContentFiltered count] == 0) {
        UIAlertView *invalidCode = [[UIAlertView alloc] initWithTitle:@"Enter a valid reference number!"
                                                              message:@"Please enter a valid reference number so we can provide the correct information for you."
                                                             delegate:nil
                                                    cancelButtonTitle:@"Close"
                                                    otherButtonTitles:nil];
        [invalidCode show];

    }

在 monoTouch 中执行此操作的最佳方法是什么?我已经尝试了一些代码,但它仍然不正确:

void GetTrackTraceData ()
        {


            //Collecting Trace and Trace data from link + security code and converting Track and Trace data into string
            string TracTraceDataString = NSString.FromData (NSData.FromUrl 
                                                    (NSUrl.FromString ("http://plex.janssen1877.com/app/life/" + NSUserDefaults.StandardUserDefaults ["SecurityToken"])), 
                                                     NSStringEncoding.UTF8);

            //Seperating the string and putting each object in the
            string[] TrackTraceDataArray = TracTraceDataString.Split (new char[] { '\n' });

            var TrackTraceFilteredArray = TrackTraceDataArray.Where (x => x.Contains (RefNr.Text));

            if (TrackTraceFilteredArray == null) {

                UIAlertView InvalidAlert = new UIAlertView ("Reference number invalid", "The reference number that you have entered is not linked to the current security code. You can change your security code in the settings.", null, "OK", null);
                InvalidAlert.Show ();
            }else{
                this.PerformSegue ("PushToTrackTraceDetails", this);
            }


            foreach (string s in TrackTraceFilteredArray)
                Console.WriteLine (s);





        }

由于某种原因,TrackTraceFilteredArray 永远不会为空。还是我必须以不同的方式实现它?这段代码是否正确编写?我是 c#/MonoTouch 的新手,所以我认为它可能需要一些优化。以下是存储在文本文件中的数据示例:

51405199 0001055479      1302180721 210005     dossier aangemaakt EDI                                                                 
51403415 0001049498      1302150503 TOMTOM101  Order 51403415-3-278065601: ontvangen                                                  
51403415 0001049498      1302151106 TOMTOM241  Order 51403415-3-278065601: gestart                                                    
51403415 0001049498      1302151106 TOMTOM249  Order 51403415-3-278065601: voltooid                                                   
51403577 0001051919      1302150503 TOMTOM101  Order 51403577-3-278065601: ontvangen                                                  
51403577 0001051919      1302151027 TOMTOM241  Order 51403577-3-278065601: gestart                                                    
51403577 0001051919      1302151039 TOMTOM241  Order 51403577-3-278065601: geschatte aankomst 10:57, 18 km op be                      
51403577 0001051919      1302151106 TOMTOM249  Order 51403577-3-278065601: voltooid                                                   
51404608 EK1000204732540 1302151708 TOMTOM101  Order 51404608-2-477100892: ontvangen                                                  
51404608 EK1000204732540 1302151708 TOMTOM101  Order 51404608-3-477100892: ontvangen                                                  
51404772 0001052637      1302150520 210005     dossier aangemaakt EDI                                                                 
51404772 0001052637      1302151732 TOMTOM101  Order 51404772-3-278065684: ontvangen                                                  
51404773 0001052688      1302150520 210005     dossier aangemaakt EDI                                                                 
51404773 0001052688      1302151732 TOMTOM101  Order 51404773-3-278065684: ontvangen                                                  
51404774 0001053872      1302150521 210005     dossier aangemaakt EDI                                                                 
51404775 0001053872      1302150521 210005     dossier aangemaakt EDI                                                                 
51404776 0001052689      1302150521 210005     dossier aangemaakt EDI                                                                 
51404777 0001052690      1302150530 210005     dossier aangemaakt EDI                                                                 
51404778 0001053934      1302150630 210005     dossier aangemaakt EDI                                                                 
51404778 0001053934      1302151733 TOMTOM101  Order 51404778-3-278065681: ontvangen                                                  
51404779 0001053934      1302150630 210005     dossier aangemaakt EDI                                                                 
51404779 0001053934      1302151733 TOMTOM101  Order 51404779-3-278065690: ontvangen                                                  
51404780 0001053934      1302150630 210005     dossier aangemaakt EDI                                                                 
51404780 0001053934      1302151733 TOMTOM101  Order 51404780-3-278065691: ontvangen
4

2 回答 2

2

LINQWhere子句不返回 1 或 null 项,它返回IEnumerablestring>0 或更多元素(这也意味着变量名 'TrackTraceFilteredArray' 不正确,因为它不是数组)。

解决此问题的一种方法是创建一个列表并检查计数:

var list = new List<string> (TrackTraceFilteredArray);
if (list.Count == 0) {
    // invalid
} else {
    // ok
}
于 2013-03-25T15:34:51.193 回答
0

而不是使用 Where,似乎你想要 Any。如果找到任何匹配项,则 Any 返回一个布尔值。这样您就不必检查计数。

于 2013-04-04T18:56:50.880 回答