为什么NSURLSessionTask.error和didCompleteWithError委托?

时间:2016-12-12 20:25:39

标签: swift swift2 nsurlsession nsurlsessiondownloadtask

我在循环中启动了许多下载,并将所有已启动的下载放入数组中以跟踪它们

当一个人成功完成后,我从数组中删除了downloadTask:

 import java.util.*;
    class Term
    {
        private int coe;
        private int pow;
        public int getCoe()
        {return coe;}
        public int getPow()
        {return pow;}
        public void setCoe(int coe)
        {this.coe=coe;}
        public void setPow(int pow)
        {this.pow=pow;}
        public Term(int coe, int pow)
        {
            this.coe=coe;
            this.pow=pow;
        }
        @Override
        public String toString()
        {
            return "(" +coe+ "," +pow+ ")";
        }
    }
    class Polynomial
    {
        private LinkedList poly;
        public Polynomial()
        {
            poly = new LinkedList();
        }
        public void addTerm(int coe, int pow)
        {
            Term t = new Term(coe,pow);
            poly.addLast(t);
        }
        public void print()
        {
            ListIterator iter = poly.listIterator();
            while(iter.hasNext())
            {
                Term temp = (Term) iter.next();
                System.out.print(temp);
            }
            System.out.println();
        }
        public void insert(int coe, int pow)
        {
            Term temp = new Term(coe,pow);
            ListIterator iter = poly.listIterator();

            while(iter.hasNext())
            {
                Term t = (Term)iter.next();
                if(t.getPow() == temp.getPow())
                {
                    t.setCoe(t.getCoe()+temp.getCoe());
                    return;
                }
                else if(t.getPow() < temp.getPow())
                {
                    iter.previous();
                    iter.add(temp);
                    return;
                }
                iter.add(temp);
            }
        }
            public Polynomial add(Polynomial p)
            {
                Polynomial a = new Polynomial();
                ListIterator iter = poly.listIterator();
                ListIterator pIter = p.poly.listIterator();

                while(iter.hasNext())
                {
                    Term t =(Term)pIter.next();
                    a.insert(t.getCoe(),t.getPow());
                }
                return a;
            }
            public Polynomial multiply(Polynomial p)
            {
                Polynomial m = new Polynomial();
                ListIterator iter = poly.listIterator();

                while (iter.hasNext())
                {
                    Term t = (Term)iter.next();
                    ListIterator pIter = (p.poly).listIterator();
                    while (pIter.hasNext())
                    {
                        Term temp = (Term)pIter.next();
                        m.insert(t.getCoe() * temp.getCoe(), t.getPow() + temp.getPow());
                    }
                }
                return m;
            }
        }

        public class PolynomialTest
        {
            public static void main (String [] args)
            {
                Polynomial p = new Polynomial();
                p.addTerm(-10,0);
                p.addTerm(-1,1);
                p.addTerm(9,7);
                p.addTerm(5,10);

                Polynomial p2;
                p2 = p.multiply(p);
                p2.print();
            }
        }

然后DownloadTask也可能失败,所以我也将它从数组中删除:

internal func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

但是有时一些下载会留在数组中,然后我发现剩下的那些下载了internal func URLSession(session: NSURLSession, downloadTask: NSURLSessionTask, didCompleteWithError error: NSError?) { 的错误属性。但为什么它不会在unknown error中触发,像这样我必须检查两个地方的错误?

对我来说这不是一个问题,我只是想知道吗?

为了扩展我的问题,我还看到你可以使用didCompleteWithError或我应该使用的completionHandler这些?是一些利弊。我个人更喜欢completionHandler,因为我会将所有代码放在一个地方,这样可以节省一些时间。

0 个答案:

没有答案