2013-03-25 75 views
0

因此,在我當前的項目中,我下載了一個包含大量數據的大型文本文件。現在我必須搜索該文件並檢查它是否包含用戶輸入的數字。在Objective-C,這將是這樣的:在monoTouch中格式化大量數據

 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的做到這一點的最好方法是什麼?我已經tryed一些代碼,但它仍然沒有正確的:

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 
+1

你能說得更具體一些嗎?陳述'它只是不正確'我們不知道什麼是對的或錯誤的問候你的預期輸出..請顯示或說明預期的輸出應該是/看起來像 – MethodMan 2013-03-25 14:27:22

+0

@DJKRAZE看到我的編輯:) – 2013-03-25 14:35:18

+0

在這一行'TracTraceDataString.Split(new char [] {'\ n'});'只是好奇,你分裂和期待一個字符串[]或一個字符[]如果你期望string []將您的代碼更改爲'TracTraceDataString.Split(new string [] {「\ n」},StringSplitOptions.RemoveEmptyEntries);'''''''' – MethodMan 2013-03-25 14:41:57

回答

0

除了使用在哪裏,好像你想要的任何的。如果找到任何匹配,任何返回一個布爾值。這樣你不必檢查計數。

2

的LINQ Where子句不返回1或零項,它返回0或多種元素的IEnumerablestring>(這也意味着變量名' TrackTraceFilteredArray'不正確,因爲它不是數組)。要解決這個

一種方法是創建一個列表,並檢查計數:

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

錯誤CS0246:無法找到類型或命名空間名稱「List」。你是否缺少using指令或程序集引用? (CS0246)(TrackandTrace) 我必須添加任何特殊的東西,然後'使用system.linq'嗎? – 2013-03-25 15:45:06

+0

必須使用'使用System.Collections.Generic;'但現在我得到這個錯誤:錯誤CS1061:鍵入'System.Collections.Generic。列表'不包含'Length'的定義,並且沒有找到'System.Collections.Generic.List '類型的擴展方法'Length'(你是否缺少using指令或程序集引用?)(CS1061) (TrackandTrace) – 2013-03-25 16:00:33

+0

對不起,它是計數,而不是長度。答案已更新。 – 2013-03-25 17:46:01