我已经定义了NSMutableArray
的属性。在致电initWithObjects
后,它count
为0.我在这里缺少什么?这是我的代码:
@interface MainViewController ()
@property NSMutableArray *photos;
@end
- (void)viewDidLoad {
[super viewDidLoad];
_photos = [_photos initWithObjects:@"1", @"2", @"3", nil];
NSLog(@"Photos count: %lu",(unsigned long)_photos.count); //prints 0
}
答案 0 :(得分:5)
这是因为您尝试调用_photos
方法时nil
为init
。请改用arrayWithObjects
:
_photos = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
答案 1 :(得分:3)
非法在init...
中说alloc
并且在同一行中说alloc
。 _photos = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
是一个类方法,你的类是NSMutableArray,所以你应该说:
public class control_it extends HttpServlet{
private static final long serialVersionUID = 1L;
public control_it () {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String name = "Display the name";
request.setAttribute("name",name);
RequestDispatcher rd=request.getRequestDispatcher("try_it.jsp");
rd.forward(request,response);
}
}