Hi all I am trying to create an array which is populated with some instances of my class BlogPost
I've started off with this :-
{
var blog : [BlogPost] = []
for item in blog{
}
am I on the right track here? just want to create 10 of these instances using a for loop any help appreciated. thanks
博客中的10个帖子
var blog = (1...10).map { _ in BlogPost() }
Here is the another way to do that:
var blog : [BlogPost] = []
for i in 1...10 {
blog.append(i)
}
for item in blog{
}
won't do anything since your array is empty. Simply, iterate n(n being the number of posts you'd like to create) times, each time creating a new object and adding it to the array;
let n : Int = 10
for(var i=0; i<n; i++){
arr.append(BlogPost())
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.