我正在学习Objective-C.我试图释放我使用的所有内存.所以,我写了一个程序来测试我是否做得对:
#import <Foundation/Foundation.h>
#define DEFAULT_NAME @"Unknown"
@interface Person : NSObject
{
NSString *name;
}
@property (copy) NSString * name;
@end
@implementation Person
@synthesize name;
- (void) dealloc {
[name release];
[super dealloc];
}
- (id) init {
if (self = [super init]) {
name = DEFAULT_NAME;
}
return self;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Person *person = [[Person alloc] init];
NSString *str;
int i;
for (i = …Run Code Online (Sandbox Code Playgroud) 为什么当我编码我可以做:
NSString *test;
test = @"stack";
test = @"overflow";
Run Code Online (Sandbox Code Playgroud)
没有任何问题,NSString是不是应该是不可改变的?
这是我的简单代码:
T a;
T b;
if (a == b)
// sth.
else
// sth. else
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我收到一个错误,说==操作符对泛型类型无效.所以我必须使用这个object.Equals()方法.
不会在==运营商实际调用Equals的方法object?为什么我可以使用Equals两种泛型类型的方法而不是==运算符?
我真的很习惯Ruby编程。我现在在用PHP工作。
我有一个对象数组,我想获取这些对象的所有ID并将它们放在数组中。
在Ruby中,您可以执行以下操作:
my_arr.map { |arr| arr.id }
Run Code Online (Sandbox Code Playgroud)
我将如何在PHP中做到这一点?我需要做一个foreach并将该ID推入数组吗?
这个方法应该以路径作为参数吗?
看起来它需要一个文件名作为路径:
例如,/ home/file.txt是一个文件,而/ home /是一个路径.这个方法看起来像前者作为参数.
我正在尝试创建一个程序来专注于某个应用程序(如果已启动)。这是我的代码:
#import <Cocoa/Cocoa.h>
#import <stdio.h>
int main() {
// activate Firefox
NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier: @"org.mozilla.firefox"];
if ([apps count] == 0) {
printf("no matching app\n");
return 1;
}
if (![apps[0] activateWithOptions: NSApplicationActivateAllWindows]) {
printf("failed to activate\n");
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它打印出“激活失败”,并且 Firefox 没有成为焦点。我究竟做错了什么?
例如,我有一个向量c(21,34,"99*",56,"90*", "45*")。我需要使用 R 来清理数据,方法是将其转换为没有额外*. 这个例子中的结果应该是c(21,34,99,56,90,45)。
我有以下代码:
#include <iostream>
using namespace std;
struct stud{
int roll;
char name[10];
double marks;
}
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){
cout<<stud1.marks<<"\n"<<endl;
cout<<stud1.name<<"\n";
cout<<stud1.roll<<"\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是有错误:
1>------ Build started: Project: array_structures1, Configuration: Debug Win32 ------
1>Build started 8/1/2010 9:26:47 PM.
1>InitializeBuildStatus:
1> Touching "Debug\array_structures1.unsuccessfulbuild".
1>ClCompile:
1> array_structures.cpp
1>c:\users\david\documents\visual studio 2010\projects\array_structures1\array_structures1\array_structures.cpp(9): error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.84
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 …Run Code Online (Sandbox Code Playgroud) objective-c ×4
cocoa ×2
c# ×1
c++ ×1
cocoa-touch ×1
generics ×1
iphone ×1
macos ×1
memory ×1
php ×1
r ×1
ruby ×1
syntax-error ×1