我正在关注这个初学者教程http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1,我有一个关于这个课程的问题及其在这里实施:
RWTScaryBugData.h
@interface RWTScaryBugData : NSObject
@property (strong) NSString *title;
@property (assign) float rating;
- (id)initWithTitle:(NSString *)title rating:(float)rating;
@end
Run Code Online (Sandbox Code Playgroud)
RWTScaryBugData.m
@implementation RWTScaryBugData
@synthesize title = _title;
@synthesize rating = _rating;
- (id)initWithTitle:(NSString *)title rating:(float)rating {
if ((self = [super init])) {
self.title = title;
self.rating = rating;
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
我正在看这个初始化器:
- (id)initWithTitle:(NSString *)title rating:(float)rating {
}
Run Code Online (Sandbox Code Playgroud)
我可以这样编写它,而不是像那样写它:
- (RWTScaryBugData *)initWithTitle:(NSString *)title rating:(float)rating {
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我区别吗?或者,如果它真的不重要?
这是reversePrint功能:
void SinglyLinkedList::reversePrint(Node* p)
{
if (p == NULL) {
return;
} else {
reversePrint(p->next);
cout << p->data << " ";
}
}
Run Code Online (Sandbox Code Playgroud)
(即:list = 1 -> 2 -> 3 -> 4
,print out = 4 3 2 1
)
它只有在*p
最初指向指向的内容时才有效*head
,即第一个节点1.但是,Node* head
SinglyLinkedList类的私有成员因此无法在类外访问.我怎样才能将a pointer
这些点传递head
给main
?
这就是我在说的:
int main() {
SinglyLinkedList *list = new SinglyLinkedList();
list->addNode(1);
list->addNode(2);
list->addNode(3);
list->addNode(4);
Node* p = head; // <---- Xcode says: Use of undeclared identifier …
Run Code Online (Sandbox Code Playgroud) 我有一个输入字符串,我想找到字符串中有多少个空格.
这是我的代码
// input string
std::string str = "abc d e f";
// convert string to cstring
char* cstr = new char[str.length()+1];
std::strcpy(cstr, str.c_str());
// iterate through the cstring and count how many spaces are there
int num_of_spaces = 0;
char* ptr = cstr;
while (ptr) {
if (*ptr == ' ') {
++num_of_spaces;
}
++ptr;
}
Run Code Online (Sandbox Code Playgroud)
但是,我在该if (*ptr == ' ')
行上收到一条错误消息:Thread 1: EXC_BAD_ACCESS (code = 1, address=0x100200000)
不是*ptr
char类型值,因为它ptr
是一个char*
指针,我将其取消引用*ptr
.如果是这样,为什么比较无效?
我说的是Integer
对象数据类型,而不是原始数据类型int
假设这样:
Integer[] arr = new Integer[5];
arr[0] = 2;
arr[1] = 2;
arr[2] = 3;
Run Code Online (Sandbox Code Playgroud)
那么数组将如下所示{2,2,3,null,null}
我怎样才能得到数组的大小?在本例中,大小为 5