如何过滤NSMutableArray SWIFT

时间:2016-12-14 10:14:01

标签: ios swift xcode filter nsmutablearray

如何在swift中过滤NSMutableArray

这是我的数组的数据

(
        (
        "http://api.preggrz.com/pics/121531121599372/65adec3fdeb58a5c24cb77be3011acd556182f66a1567.jpg",
        "http://api.preggrz.com/pics/121531121599372/8e292eb776a62383eff8dc0f43ad5988fc2cee15d542d.jpg",
        "http://api.preggrz.com/pics/121531121599372/bf98f74fa24c467ae686ace1676e2e48d7c9d67ccbf8a.jpg",
        "http://api.preggrz.com/pics/121531121599372/cf48d39f51511e942f8b8f7e9cf6095c5b38c9228645c.jpg",
        "http://api.preggrz.com/pics/121531121599372/fbe5c86f048ece3a3edebf786f30613986ac6f1d97aed.jpg"
    ),
        (
        "http://api.preggrz.com/pics/10154319685331226/032e9.jpg",
        "http://api.preggrz.com/pics/10154319685331226/0a021363c2820f008968df61340844512d2ea79b781ca.jpg",
        "http://api.preggrz.com/pics/10154319685331226/0ca77.jpg",
        "http://api.preggrz.com/pics/10154319685331226/133b5b677b4adb2e854822cb13f7afcdb66b0b7d117e8.jpg",
        "http://api.preggrz.com/pics/10154319685331226/19baf33927a241299a89069b7f953810a55d58bb73c4f.jpg"
    ),
        (
        "http://api.preggrz.com/pics/111896919230617/13878a5cb249a597d6aa72436032f6d535455cbcc76fd.jpg",
        "http://api.preggrz.com/pics/111896919230617/9a702645cbdb7dd4565d6e3e8151b1bf32b88d7854453.jpg",
        "http://api.preggrz.com/pics/111896919230617/b671642c6aaa09fae96dc437b06ca92fc8f83e6c0bf66.jpg",
        "http://api.preggrz.com/pics/111896919230617/c03d17e9d513175390138c8404ec9052f0f50955936ac.jpg",
        "http://api.preggrz.com/pics/111896919230617/f05950e92b3b4136d559b65b30bb7dc01d5a10eb7f864.jpg"
    ),
        (
        "http://api.preggrz.com/pics/104262049996560/00f87e416ba5f511df29bf327be0dbd15d4c37e236d97.jpg",
        "http://api.preggrz.com/pics/104262049996560/02215e416ba5f511df29bf327be0dbd15d4c37e236d97.jpg",
        "http://api.preggrz.com/pics/104262049996560/0414f804ac4caaecc484c902cdf8ef52147ddfefa171b.jpg",
        "http://api.preggrz.com/pics/104262049996560/123898ad2dc2efd4e50744d93890441e39acce59a8d6f.jpg",
        "http://api.preggrz.com/pics/104262049996560/15559.jpg"
    ),
        (
        "http://api.preggrz.com/pics/111009959320285/1e0a86087ce3f425a0a257ec3da89ca5b6e98ed86864d.jpg",
        "http://api.preggrz.com/pics/111009959320285/4abff6a73c9fc9b6369cdc1df217fc05823f243e89822.jpg",
        "http://api.preggrz.com/pics/111009959320285/565f2e9f3b259e131db2eca776259daaa08912355558a.jpg",
        "http://api.preggrz.com/pics/111009959320285/605d06087ce3f425a0a257ec3da89ca5b6e98ed86864d.jpg",
        "http://api.preggrz.com/pics/111009959320285/89b6a018955e19110b86195f0d1af693c0415add69bbe.jpg"
    ),
        (
        "http://api.preggrz.com/pics/120936158325602/1cd7b29d83553396d2da77abcc9e072b43eb07739f800.jpg",
        "http://api.preggrz.com/pics/120936158325602/3eb56e0b539512383483e9e817ea5bb853dbda2c50f81.jpg",
        "http://api.preggrz.com/pics/120936158325602/4693fe0b539512383483e9e817ea5bb853dbda2c50f81.jpg",
        "http://api.preggrz.com/pics/120936158325602/613a09e4d95c89fb24858622c770db7e3de3ea76218ba.jpg",
        "http://api.preggrz.com/pics/120936158325602/6361d9e4d95c89fb24858622c770db7e3de3ea76218ba.jpg"
    ),
        (
        "http://api.preggrz.com/pics/101520963605089/00d24f7b7f1f9b9eed50291eae536025bf9d43326d6ca.jpg",
        "http://api.preggrz.com/pics/101520963605089/049da5cd1ac6b22364e22d98bab30949f534d39d7b2ed.jpg",
        "http://api.preggrz.com/pics/101520963605089/04be4f7b7f1f9b9eed50291eae536025bf9d43326d6ca.jpg",
        "http://api.preggrz.com/pics/101520963605089/076eaf7570a058c3a55c5260b6dbe3476b8f69e7d1c01.jpg",
        "http://api.preggrz.com/pics/101520963605089/0c87ab59f22cf20807326f15da6970af2586a16296f58.jpg"
    ),
        (
        "http://api.preggrz.com/pics/108266076261880/0441fdf186cebef98b6d2a5d9457abfcf248789e05347.jpg",
        "http://api.preggrz.com/pics/108266076261880/6e6afee3c37663d073c38485704c6284551ca376d48bc.jpg",
        "http://api.preggrz.com/pics/108266076261880/a0eb79b908ecd2b80cf82f0f03123377addf03977988c.jpg",
        "http://api.preggrz.com/pics/108266076261880/be09d49122ceb9dbba6583844ac2d8e84ded65e4b5155.jpg",
        "http://api.preggrz.com/pics/108266076261880/fa1ffe456fdf25ab4fd9a6ece148a2d4a841a7f357de5.jpg"
    ),
        (
        "http://api.preggrz.com/pics/115124662241251/07156.jpg",
        "http://api.preggrz.com/pics/115124662241251/1f670a26cdc8c32c7f7da4b5a9717a231cc3bb21dfe2f.jpg",
        "http://api.preggrz.com/pics/115124662241251/23546a26cdc8c32c7f7da4b5a9717a231cc3bb21dfe2f.jpg",
        "http://api.preggrz.com/pics/115124662241251/24603899268824db5bbb325ea974fa740981b24f7bdc6.jpg",
        "http://api.preggrz.com/pics/115124662241251/32717.jpg"
    )

我想根据我要输入的字符串过滤它。感谢您的回复。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情(代码是自我解释或评论):

func filteredStrings(input: String) -> [[String]] {
        let items: [[String]] = [
            [
                "http://api.preggrz.com/pics/121531121599372/65adec3fdeb58a5c24cb77be3011acd556182f66a1567.jpg",
                "http://api.preggrz.com/pics/121531121599372/8e292eb776a62383eff8dc0f43ad5988fc2cee15d542d.jpg",
                "http://api.preggrz.com/pics/121531121599372/bf98f74fa24c467ae686ace1676e2e48d7c9d67ccbf8a.jpg",
                "http://api.preggrz.com/pics/121531121599372/cf48d39f51511e942f8b8f7e9cf6095c5b38c9228645c.jpg",
                "http://api.preggrz.com/pics/121531121599372/fbe5c86f048ece3a3edebf786f30613986ac6f1d97aed.jpg"
            ],
            [
                "http://api.preggrz.com/pics/10154319685331226/032e9.jpg",
                "http://api.preggrz.com/pics/10154319685331226/0a021363c2820f008968df61340844512d2ea79b781ca.jpg",
                "http://api.preggrz.com/pics/10154319685331226/0ca77.jpg",
                "http://api.preggrz.com/pics/10154319685331226/133b5b677b4adb2e854822cb13f7afcdb66b0b7d117e8.jpg",
                "http://api.preggrz.com/pics/10154319685331226/19baf33927a241299a89069b7f953810a55d58bb73c4f.jpg"
            ],
            [
                "http://api.preggrz.com/pics/111896919230617/13878a5cb249a597d6aa72436032f6d535455cbcc76fd.jpg",
                "http://api.preggrz.com/pics/111896919230617/9a702645cbdb7dd4565d6e3e8151b1bf32b88d7854453.jpg",
                "http://api.preggrz.com/pics/111896919230617/b671642c6aaa09fae96dc437b06ca92fc8f83e6c0bf66.jpg",
                "http://api.preggrz.com/pics/111896919230617/c03d17e9d513175390138c8404ec9052f0f50955936ac.jpg",
                "http://api.preggrz.com/pics/111896919230617/f05950e92b3b4136d559b65b30bb7dc01d5a10eb7f864.jpg"
            ],
            [
                "http://api.preggrz.com/pics/104262049996560/00f87e416ba5f511df29bf327be0dbd15d4c37e236d97.jpg",
                "http://api.preggrz.com/pics/104262049996560/02215e416ba5f511df29bf327be0dbd15d4c37e236d97.jpg",
                "http://api.preggrz.com/pics/104262049996560/0414f804ac4caaecc484c902cdf8ef52147ddfefa171b.jpg",
                "http://api.preggrz.com/pics/104262049996560/123898ad2dc2efd4e50744d93890441e39acce59a8d6f.jpg",
                "http://api.preggrz.com/pics/104262049996560/15559.jpg"
            ],
            [
                "http://api.preggrz.com/pics/111009959320285/1e0a86087ce3f425a0a257ec3da89ca5b6e98ed86864d.jpg",
                "http://api.preggrz.com/pics/111009959320285/4abff6a73c9fc9b6369cdc1df217fc05823f243e89822.jpg",
                "http://api.preggrz.com/pics/111009959320285/565f2e9f3b259e131db2eca776259daaa08912355558a.jpg",
                "http://api.preggrz.com/pics/111009959320285/605d06087ce3f425a0a257ec3da89ca5b6e98ed86864d.jpg",
                "http://api.preggrz.com/pics/111009959320285/89b6a018955e19110b86195f0d1af693c0415add69bbe.jpg"
            ],
            [
                "http://api.preggrz.com/pics/120936158325602/1cd7b29d83553396d2da77abcc9e072b43eb07739f800.jpg",
                "http://api.preggrz.com/pics/120936158325602/3eb56e0b539512383483e9e817ea5bb853dbda2c50f81.jpg",
                "http://api.preggrz.com/pics/120936158325602/4693fe0b539512383483e9e817ea5bb853dbda2c50f81.jpg",
                "http://api.preggrz.com/pics/120936158325602/613a09e4d95c89fb24858622c770db7e3de3ea76218ba.jpg",
                "http://api.preggrz.com/pics/120936158325602/6361d9e4d95c89fb24858622c770db7e3de3ea76218ba.jpg"
            ],
            [
                "http://api.preggrz.com/pics/101520963605089/00d24f7b7f1f9b9eed50291eae536025bf9d43326d6ca.jpg",
                "http://api.preggrz.com/pics/101520963605089/049da5cd1ac6b22364e22d98bab30949f534d39d7b2ed.jpg",
                "http://api.preggrz.com/pics/101520963605089/04be4f7b7f1f9b9eed50291eae536025bf9d43326d6ca.jpg",
                "http://api.preggrz.com/pics/101520963605089/076eaf7570a058c3a55c5260b6dbe3476b8f69e7d1c01.jpg",
                "http://api.preggrz.com/pics/101520963605089/0c87ab59f22cf20807326f15da6970af2586a16296f58.jpg"
            ],
            [
                "http://api.preggrz.com/pics/108266076261880/0441fdf186cebef98b6d2a5d9457abfcf248789e05347.jpg",
                "http://api.preggrz.com/pics/108266076261880/6e6afee3c37663d073c38485704c6284551ca376d48bc.jpg",
                "http://api.preggrz.com/pics/108266076261880/a0eb79b908ecd2b80cf82f0f03123377addf03977988c.jpg",
                "http://api.preggrz.com/pics/108266076261880/be09d49122ceb9dbba6583844ac2d8e84ded65e4b5155.jpg",
                "http://api.preggrz.com/pics/108266076261880/fa1ffe456fdf25ab4fd9a6ece148a2d4a841a7f357de5.jpg"
            ],
            [
                "http://api.preggrz.com/pics/115124662241251/07156.jpg",
                "http://api.preggrz.com/pics/115124662241251/1f670a26cdc8c32c7f7da4b5a9717a231cc3bb21dfe2f.jpg",
                "http://api.preggrz.com/pics/115124662241251/23546a26cdc8c32c7f7da4b5a9717a231cc3bb21dfe2f.jpg",
                "http://api.preggrz.com/pics/115124662241251/24603899268824db5bbb325ea974fa740981b24f7bdc6.jpg",
                "http://api.preggrz.com/pics/115124662241251/32717.jpg"
        ]]

        return items.flatMap { stringArray in
            let newArray: [String] = stringArray.filter{ $0.contains(input) } // The inner array of filtered strings. If the string contains an "input" then preserve it
            return newArray.count > 0 ? newArray : nil // If the filtered array is empty do not include it.
        }
    }