0
class abc{
    var name = String()

     init(name:String){
        self.name = name
     }
}

var obj = [abc]()
obj[0] = abc(name:"sun")
obj[1] = abc(name:"sharma")
2
  • i am trying to stroe multiple data to create array of object class Commented Dec 13, 2017 at 17:09
  • 1
    Even though it's quite obvious in this case what's wrong, you should always explain what is not working and include any compile-/runtime errors in your question along with the expected/actual behaviour. Especially since all questions on SO should be useful for future readers, which won't be the case without these pieces of information. Commented Dec 13, 2017 at 17:15

2 Answers 2

2

The issue is that you are trying to access elements of the array by subscript that don't exist by the time you're trying to access them.

var obj = [abc]() just initializes an empty array, so you cannot access its elements by subscript, since it doesn't have any elements yet.

You should use Array.append to add new elements to the end of your array.

var obj = [Abc]()
obj.append(Abc(name:"sun"))
obj.append(Abc(name:"sharma"))

You can also create the array straight away with the elements you want to store in it:

var obj = [Abc(name:"sun"),Abc(name:"sharma")]

After you have populate the array, you can access its elements in several ways. If you want to iterate through the array, you'll usually want to use the for...in.. loop:

for object in obj {
    print(object.name)
}

If you want to access a single element, you can do that using array subscripts, but make sure the index doesn't exceed the array's size before using indexing.

let index = 1
if index < array.count { //safe to use index as subscript
    print(obj[index])
}

You should also conform to the Swift naming convention, which is upperCamelCase for types (Abc instead of abc).

Sign up to request clarification or add additional context in comments.

1 Comment

yes it is working now tell how can access this elements
0

I think you are asking about creating an array of objects..

It is really easy

Consider you have two classes, Class A and Class B

Class A{
    var name:String

    init(localName: String){
        self.name = localName
    }
}

Class B{
    //here you can create an array of objects of class A
    //this will create an empty array of objects of class A
    var objList = [A]()

    //to add data into this array u can use append 
    func addData(objA: A){
        self.objList.append(objA)
    }

    func displayData(list : [A]){
        for entry in list{    
            print(entry.name)
        }
    }

}

1 Comment

You shouldn't be using implicitly unwrapped optionals unless you have a very good reason to do so (i.e. defining an IBOutlet). name can simply be a String, there's no reason to make it String!... Please also make sure you properly format your code, otherwise it's really hard to read it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.