Access local file path in Objective C

时间:2017-12-18 07:26:27

标签: ios xml

I have added a folder name Resources and under it there is another folder name File inside the file folder there are two xml files which i'm trying to parse. When i give pathforResource method name to my Resources folder it shows path is null. How can i access the two xml file from these folders? Resources->File->text.xml. My code is this,

 NSMutableArray *aryXMLName = [NSMutableArray new];
[aryXMLName addObject:@"text.xml"];
[aryXMLName addObject:@"test1.xml"];

for(NSString *str in aryXMLName){
    NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:@"Resources"];
    NSLog(@"Path : %@",path);
    NSData *data=[NSData dataWithContentsOfFile:path];

    if ([str isEqualToString:@"text.xml"]) {
        if (data == nil)
            return;
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"string: %@", str);
        NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];
        NSLog(@"dictionary: %@", xmlDoc);
    }else if ([str isEqualToString:@"test1.xml"]){
        if (data == nil)
            return;
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"string: %@", str);
        NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];
        NSLog(@"dictionary: %@", xmlDoc);
    }


}

The scenario is like this,

enter image description here

1 个答案:

答案 0 :(得分:0)

Replace this code:

 @objc func fetchArticles(fromSource provider: String) {
         let urlRequest = URLRequest(url: URL(string:"https://newsapi.org/v2/top-headlines?category=\(provider)&language=en&apiKey=apikeyremoved")!)



        let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in

            if error != nil {
                print(error as Any)
                return
            }

            self.articles = [Article]()
            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
                if let articlesFromJson = json["articles"] as? [[String : AnyObject]]{
                    for articleFromJson in articlesFromJson {
                        let article = Article()
                        if let title = articleFromJson["title"] as? String, let author = articleFromJson["author"] as? String, let desc = articleFromJson["description"] as? String,let url = articleFromJson["url"] as? String, let urlToImage = articleFromJson["urlToImage"] as? String {

                            article.author = author
                            article.desc = desc
                            article.headline = title
                            article.url = url
                            article.imageURL = urlToImage

                        }
                        self.articles?.append(article)

                    }
                }
                DispatchQueue.main.async {
                    self.collectionview.reloadData()
                }


            }catch let error {
                print(error)
            }

        }

        task.resume()
    }