Swift - 函数仅在从字典附加键值后返回 nil



New to Swift. 对类有一定的了解。 我有一个getSomething()函数,它应该返回从字典附加的键值数组(不是值本身,而只是它们的键(。 但是,我的函数只返回nil

public class MyClass {
            private var somethingA : String
            private var somethingB : String
            private var somethingC : Int
            // A dictionary
            // For example, if I add a fruit, color and price
            // ["Apple":["red":5]]
            private var complexes:[String:[String:Int]] = [String:[String:Int]]();
            init() {
                 self.somethingA = "";
                 self.somethingB = "";
                 self.somethingC = 0;
                 self.complexes = [somethingA:[somethingB:somethingC]];
            }
            // Adds a string to the dict complexes
            // For example, ["Apple] = ["", 0]
            public func addSomething(somethingAA : String) {
                 self.somethingA = somethingAA;
                 self.complexes[somethingAA] = [self.somethingB : self.somethingC];
            }
            // Adds a whole entry to the dict complexes
            // For example, ["Apple"] = ["red": 5]
            public func addComplex(somethingAA:  String, complex:(somethingBB:  String,  somethingCC:  Int)) {
                self.somethingA = somethingAA
                self.somethingB = complex.somethingBB
                self.somethingC = complex.somethingCC
                complexes[somethingAA] = [complex.somethingBB: complex.somethingCC]
            }
            // My Problem:  only returns nil
            public func getSometing() -> [String]?{
                 var myArray:[String]? = nil;
                 let keys = Array(complexes.keys);
                 for key in keys { myArray?.append(key); } // This may be the line of code that isn't properly working
                 return myArray;
            }    
        }

我的测试文件:

 var sampleObject:MyClass;
 sampleObject = MyClass();
 sampleObject.addSomething(somethingAA:  "Apple");
 sampleObject.addSomething(somethingAA:  "Orange");
 print(sampleObject.getSomething());

我的getSomething()函数仅打印nil这意味着它不会将正确的keys附加到myArray

不知道为什么。 自从这是我使用 Swift 的头几天以来,我并没有真正工作过或熟悉nils,但我在 Java 和 C# 中使用过nullsnils等同于nulls吗?

我想要的正确输出应该返回如下:

["Apple", "Orange"] // not necessarily in order

您尚未初始化数组。所以应该是这样:

public func getSometing() -> [String]?{
   var myArray = [String]();
   let keys = Array(complexes.keys);
   for key in keys { myArray?.append(key); } // This may be the line of code that isn't properly working
   return myArray;
   }

你的 getSometing(( 函数应该是这样的,

public func getSometing() -> [String]?{
   var myArray:[String] = [];
   let keys = Array(complexes.keys);
   for key in keys { myArray.append(key);}
   return myArray;
}

你的还没有实例化myArray。尝试

var myArray:[String]? = [String]();

问题出在您的getSomething方法中:

// this is nil, there is no object in myArray, because you initialise it to nil
var myArray:[String]? = nil;
let keys = Array(complexes.keys);
for key in keys { myArray?.append(key); } // since myArray is nil, this does nothing
// and you return nil
return myArray;

因此,请正确初始化数组:

var myArray:[String]? = [] // this will create new empty array

或者更简单:

public func getSomething() -> [String]?{
    return Array(complexes.keys)
}

相关内容

  • 没有找到相关文章

最新更新