xcode 在Objective C中从整数制作指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8628279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making pointer from integer in Objective C
提问by DigiOz Multimedia
After programming with C# and Java for many years I finally decided to learn Objective-C in order to start programming iOS Devices as well as Mac OS X, and I have to admit it is very different then most modern c-based programming languages. I am getting the following warning in my code:
在使用 C# 和 Java 编程多年之后,我终于决定学习 Objective-C,以便开始对 iOS 设备和 Mac OS X 进行编程,我不得不承认它与大多数现代基于 c 的编程语言非常不同。我在我的代码中收到以下警告:
warning: passing argument 1 of 'SetAge' makes pointer from integer without a cast
警告:传递“SetAge”的参数 1 使指针从整数而不进行强制转换
Here is my code:
这是我的代码:
Dog.h
狗.h
#import <Cocoa/Cocoa.h>
@interface Dog : NSObject {
int ciAge;
NSString * csName;
}
- (void) Bark;
- (void) SetAge: (int *) liAge;
- (void) SetName: (NSString *) lsName;
@end
Dog.m
狗.m
#import "Dog.h"
@implementation Dog
- (void) Bark
{
NSLog(@"The dog %@ barks with age %d", csName, ciAge);
}
- (void) SetAge: (int *) liAge {
ciAge = (int)liAge;
}
- (void) SetName: (NSString *) lsName {
csName = lsName;
}
@end
HelloWorld.m
你好世界
#import <Foundation/Foundation.h>
#import "Dog.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int liTemp = 75;
NSString * lsCity = @"Chicago";
NSDate * loDate = [NSDate date];
// insert code here...
NSLog(@"The temperature is %d currently in %@, on %@", liTemp, lsCity, loDate);
int liAge = 10;
// Call Dog class
Dog * Dog1 = [Dog new];
[Dog1 SetAge:(int)liAge]; // The Warning happens here
[Dog1 SetName:@"Fido"];
[Dog1 Bark];
[pool drain];
return 0;
}
My Questions Are:
我的问题是:
- How do I get rid of the warning above?
- Instead of creating methods in the Dog Class for setting Age and Name, how could I have make Age and Name public class level variables that I could directly assign to?
- 我如何摆脱上面的警告?
- 不是在 Dog 类中创建设置 Age 和 Name 的方法,而是如何让 Age 和 Name 公共类级别的变量可以直接分配给它?
Any help would be much appreciated!
任何帮助将非常感激!
Thanks, Pete
谢谢,皮特
回答by Jeremy
Don't declare an int as a pointer. Change your code from:
不要将 int 声明为指针。更改您的代码:
- (void) SetAge: (int *) liAge
to
到
- (void) SetAge: (int) liAge
and
和
- (void) SetAge: (int *) liAge {
ciAge = (int)liAge;
}
to
到
- (void) SetAge: (int) liAge {
ciAge = liAge;
}
Consider making age and name a property. Change:
考虑制作年龄并命名属性。改变:
- (void) SetAge: (int *) liAge;
- (void) SetName: (NSString *) lsName;
to
到
@property (nonatomic, readwrite) NSInteger age; //Don't declare as pointer
@property (nonatomic, retain) NSString *name; //DO declare as pointer
Also, don't forget to synthesize them in your implementation file:
另外,不要忘记在您的实现文件中综合它们:
@synthesize age, name;
回答by thomas.g
int
is a C primitive scalar type so you don't need pointers. Don't use *
.
int
是 C 原始标量类型,因此您不需要指针。不要使用*
.
Warning here
此处警告
int *
represents a pointer variable, a thing you're not used to see in C# or in Java.
int *
代表一个指针变量,这是你在 C# 或 Java 中不习惯看到的东西。
int *p
is a variable that will point to a memory address. To put data at this address you have to dereference the variable p before using it ex:*p = 3
.
int *p
是一个指向内存地址的变量。要将数据放在此地址,您必须在使用变量 p 之前取消引用它 ex: *p = 3
。
Objective-C is based on the C language and this is a C language problem. You should (must ?) read about C and pointers if you want to code in Objective-C.
Objective-C 基于 C 语言,这是一个 C 语言问题。如果你想在 Objective-C 中编码,你应该(必须?)阅读 C 和指针。
And read also how Objective-C simplifies your life with pointers to objects particularly the fact that you don't have do explicitly dereference them to use them.
并阅读 Objective-C 如何通过指向对象的指针简化您的生活,特别是您没有明确取消引用它们以使用它们的事实。