在monoTouch中格式化大量数据

本文关键字:数据 格式化 monoTouch | 更新日期: 2023-09-27 18:05:36

所以在我当前的项目中,我下载了一个包含大量数据的大型文本文件。现在我必须搜索该文件并检查它是否包含用户输入的数字。在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

在monoTouch中格式化大量数据

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

解决这个问题的一种方法是创建一个列表并检查计数:
var list = new List<string> (TrackTraceFilteredArray);
if (list.Count == 0) {
    // invalid
} else {
    // ok
}

不是使用Where,而是使用Any。如果找到Any匹配,Any返回bool值。这样你就不用检查点数了。