繁体   English   中英

在monoTouch中格式化大量数据

[英]Formating a large amount of data in monoTouch

所以在我目前的项目中,我下载了一个包含大量数据的大型文本文件。 现在我必须搜索该文件并检查它是否包含用户输入的数字。 在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永远不会为null。 或者我必须以不同的方式实现这一点吗? 这段代码是否写得正确? 我是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

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

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

var list = new List<string> (TrackTraceFilteredArray);
if (list.Count == 0) {
    // invalid
} else {
    // ok
}

而不是使用Where,似乎你想要Any。 如果找到任何匹配,则任何返回bool。 这样你就不用检查计数了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM