自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在iPhone中如何從Application Bundle中讀取文件

移動開發(fā) iOS
本文介紹的是在iPhone中如何從Application Bundle中讀取文件,文章以代碼實現(xiàn),先來看內(nèi)容。

iPhone中如何從Application Bundle中讀取文件死本文要介紹的內(nèi)容,本文是代碼實現(xiàn)結(jié)果。先來看內(nèi)容詳解。首先必須將文件加入Xcode工程的Resources目錄。然后可以如下訪問文件,假設(shè)文件為MyFile.txt:

Java代碼

  1.   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
  2.   NSData *myData = [NSData dataWithContentsOfFile:filePath];  
  3.   if (myData) {  
  4.   // do something useful  
  5.   }  
  6.   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
  7.   NSData *myData = [NSData dataWithContentsOfFile:filePath];  
  8.   if (myData) {  
  9.   // do something useful  
  10.   } 

一段將help文本文件讀入UIWebView的完整示例:

Java代碼

  1.   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];  
  2.   NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
  3.   if (htmlData) {  
  4.   [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];  
  5.   }  
  6.   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];  
  7.   NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
  8.   if (htmlData) {  
  9.   [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];  
  10.   } 

如果想將文件讀入字符串,則可以用UITextView顯示,例如:

Java代碼

  1.   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];  
  2.   if (filePath) {  
  3.   NSString *myText = [NSString stringWithContentsOfFile:filePath];  
  4.   if (myText) {  
  5.   textView.textmyText;  
  6.   }  
  7.   }  
  8.   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];  
  9.   if (filePath) {  
  10.   NSString *myText = [NSString stringWithContentsOfFile:filePath];  
  11.   if (myText) {  
  12.   textView.textmyText;  
  13.   }  
  14.   } 

小結(jié):在iPhone中如何從Application Bundle中讀取文件的內(nèi)容介紹完了希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-11-05 13:02:58

內(nèi)存iPhone

2022-09-27 10:07:01

要使用 source

2009-06-16 15:20:48

ApplicationJSP程序

2019-09-24 20:07:30

Linux移動文件 命令

2019-03-18 09:40:44

Linux命令編程語言

2023-01-15 17:11:44

Rust

2021-04-21 08:03:34

腳本Shell讀取

2021-08-20 10:46:25

Shell腳本文件Linux

2010-04-20 10:12:42

OraclePL

2011-07-26 14:18:20

2011-08-22 11:28:46

iPhone開發(fā)BMP

2020-09-30 08:26:33

Spring Boot

2011-08-12 14:33:06

iPhone緩存文件

2011-07-29 13:27:48

iPhone 開發(fā) Nib

2009-06-29 17:57:30

ApplicationJSP

2018-06-01 10:37:26

Linux歸檔文件壓縮文件

2009-09-29 09:16:23

啟動bundleOSGi

2012-05-18 12:49:43

Android

2010-08-06 13:22:48

FlexCSS

2011-10-27 14:15:05

Java 7
點贊
收藏

51CTO技術(shù)棧公眾號