简体   繁体   English

Swift 2:如何将动态数目的字符串分配给字符串数组?

[英]Swift 2: How can I assign a dynamic number of strings to an array of Strings?

Context: 语境:

I'm attempting to create an app that populates a UICollectionView (tvOS) based on the contents of a folder in a local server I'm running. 我正在尝试创建一个基于正在运行的本地服务器中的文件夹内容填充UICollectionView(tvOS)的应用程序。 The way I'm trying to do this (and it may be completely wrong way) is to 'scrape' the html of my server pages. 我尝试执行此操作的方法(可能是完全错误的方法)是“擦除”服务器页面的html。

I have the following property defined: 我定义了以下属性:

let moviesArray: [String] = ["<li>Example 1</li>", "<li>Example 2</li>"]

My UICollectionView will return the number of UICollectionViewCell's equal to moviesArray.count so I need this number (along with additional data that I won't speak to in this question). 我UICollectionView将返回UICollectionViewCell的相等数量moviesArray.count所以我需要这个数(与我不会在这个问题上的话语以及其他数据)。

Problem: 问题:

The number of files/folders I add to my server is dynamic and will increase / decrease depending on what movies I add to it or delete from it. 我添加到服务器中的文件/文件夹的数量是动态的,并且会根据我添加或删除的电影数量而增加/减少。 This will obviously change the html which is returned. 显然,这将更改返回的html。

I have the following piece of code that returns the html of my local server's URL and converts it to a string. 我有以下代码段,这些代码返回本地服务器URL的html并将其转换为字符串。 I then create a substring of that based on a range, and then split out each individual <li> element from the html. 然后,我根据范围创建该字符串的子字符串,然后从html中拆分出每个单独的<li>元素。 Here's the code: 这是代码:

     do {

        let url:NSURL? = NSURL(string: "http://localhost:8888/Files/movies/")

        // 'contentsOfURL' returns the html code of the page
        let html:String? = try NSString(contentsOfURL: url!, encoding: NSUTF8StringEncoding) as String
        print(html)

        // creates a substring of 'html' based on a range defined below
        let ulString = html![(html?.startIndex.advancedBy(164))!...(html?.endIndex.advancedBy(-22))!]
        print("<ul> substring is: \(ulString)")

        // Splits each <li> into a separate string
        let liSplit = ulString.characters.split{$0 == "\n"}.map(String.init)
        print("li 1 is: \(liSplit[0])")
        print("li 2 is: \(liSplit[1])")
        print("li 3 is: \(liSplit[2])")

    } catch {
        print("the error is: \(error)")
    }

My Question: 我的问题:

How can I add or remove String objects to self.moviesArray based on the number of <li> elements that are in the html string which is returned? 如何根据返回的html字符串中<li>元素的数量将String对象添加到self.moviesArrayself.moviesArray删除? How will I assign each member of liSplit to self.moviesArray if it is a dynamic number? 如果它是一个动态数字,如何将liSplit每个成员分配给self.moviesArray

You need to declare you moviesArray as variable, not as constant and initialize it: 您需要将moviesArray声明为变量,而不是常量,然后将其初始化:

var moviesArray = [String]()

Then you can go ahead and simply add all the retrieved element to it: 然后,您可以继续并简单地将所有检索到的元素添加到其中:

moviesArray += liSplit

There are two types of Array in Objective-C, 1) NSArray 2) NSMutableArray 在Objective-C中有两种类型的数组, 1) NSArray 2) NSMutableArray

You can't add objects to or remove objects from NSArray. 您不能向NSArray添加对象或从中删除对象。 You can add objects to or remove objects from NSMutableArray. 您可以向NSMutableArray添加对象或从中删除对象。

In swift, we have only one Array, ie Array eg Array<String> or [String], Array<NSURL> or [NSURL], etc 很快,我们只有一个数组,即Array例如Array<String> or [String], Array<NSURL> or [NSURL], etc

If you declare array with var keyword, it will be mutable version, so you can add or remove objects eg 如果用var关键字声明数组,它将是可变版本,因此您可以添加或删除对象, 例如

var arrayString : Array<String> = Array()
arrayString.append("Hitendra")
arrayString.append("Solanki")

If you declare array with let keyword, it will not be mutable version, so you can't add or remove objects eg 如果用let关键字声明array,它将不是可变版本,因此您不能添加或删除对象, 例如

var arrayString : Array<String> = Array()
arrayString.append("Hitendra")//not allowed
arrayString.append("Solanki")//not allowed

So just change the 所以只要改变

let moviesArray: [String] = ["<li>Example 1</li>", "<li>Example 2</li>"]

to

var moviesArray: [String] = ["<li>Example 1</li>", "<li>Example 2</li>"]

and use following lines, 并使用以下几行

let liSplit = ulString.characters.split{$0 == "\n"}.map(String.init)

for listItem in liSplit {
moviesArray.append(listItem)
}

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

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