所以基本上我正在使用window.location ="myobj:mymethod:myarg:myotherarg"来实现在objc中处理JavaScript调用的典型方法,但是,我想知道是否有办法将一个参数数组应用于一个方法,类似于你在JavaScript中的方式.
通常我一直在做
-(void) mymethod:(NSArray*) arr{
//method knows how many arguments it takes and what they mean at each index
}
Run Code Online (Sandbox Code Playgroud)
我更愿意这样做:
-(void) mymethod:(NSString*) myarg myOtherArg: (NSString*) myotherarg{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
并有一个这样的方法:
+(void) callMethod:(NSString*)selectorName withArgs: (NSArray*)args onObject:(id) obj{
//implementation
}
[JEHelpers callMethod:selector withArgs:someArrayOfArgs onObject:myapp]
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我有一个带有架构的模型:
schema = new Schema({
name: String,
sections: [{
name: String,
columns: [{
name: String
}]
}]
}];
//Lets call the model a Page
Run Code Online (Sandbox Code Playgroud)
为简单起见,我检索整个模型:
app.get('/page/:name', function(req, res){
Page.findOne( {name: req.params.name}, function(err, page){
//error handling
res.send page
});
});
Run Code Online (Sandbox Code Playgroud)
通过请求 GET /page/myPage 我收到:
{
//_id, __v, etc..
name: 'myPage',
sections: [{
name: 'Section 1',
columns [{
name: 'My #1 Column'
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
我将 0 列名称更改为我的 #1 列 FOOBAR!在客户端
{
//_id, __v, etc..
name: 'myPage',
sections: [{
name: 'Section 1', …Run Code Online (Sandbox Code Playgroud) 当使用NSMutableArray中的新值替换某个索引处的值时,旧值将保留在内存中.修复的方法是在每个循环之前初始化一个新的NSMutableArray.
重现步骤:
- (id) init{
self.overlays = [[NSMutableArray alloc] initWithCapacity: [self.anotherArray count]];
}
- (void) someOtherMethod{
for(int i = 0 ; i < self.anotherArray ; i++){
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor colorWithRed:0
green:0
blue:0
alpha:1]];
[view setAlpha: .2];
[self.overlays insertObject:view atIndex: i]
}
}
- (void) main{
for(int i = 0 ; i < 4 ; i++){
[myObject someOtherMethod];
}
}
Run Code Online (Sandbox Code Playgroud)
insertObject:atIndex有效地导致内存泄漏,因为它不会释放该索引处的数组中的旧值.
我提交了一份错误报告,Apple回答说:
insertObject:atIndex:表现为已定义.它正在插入,而不是替代.如果要替换,则应使用-replaceObjectAtIndex:withObject:
insertObject:atIndex:怎么可能有任何好处,因为你总是丢失对该索引的旧对象的引用.
这是否只是为了避免修复问题,因为它符合旧的文档定义?