I get this exception when i try to create a new layer. So , basically i have a standart cocos2d template , i just removed HelloWorldLayer class and created a new class called GameScene, were i defined +scene method , init and dealloc methods
GameScene.h
#import "cocos2d.h"
#interface GameScene : CCLayer {}
+(id) scene;
#end
GameScene.m
#import "GameScene.h"
#implementation GameScene
+(id) scene {
CCScene *scene = [CCScene node];
GameScene *layer = [GameScene node];
[scene addChild:layer];
return self;
}
-(id) init {
if ((self = [super init])) {
CCLOG(#"New GameScene");
}
return self;
}
-(void) dealloc {
[super dealloc];
}
#end
I my appDelegate , instead of old layer , create new one from GameScene
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
…
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [GameScene scene]];
}
Console output shows :
2012-02-06 20:37:04.284 Doodle[3908:10a03] +[GameScene onEnter]: unrecognized selector sent to class 0x1098c4
2012-02-06 20:37:04.288 Doodle[3908:10a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[GameScene onEnter]: unrecognized selector sent to class 0x1098c4'
I figure it out. Instead of returning an id type for method +scene , it should be a CCScene value.
// in header also modify type of returned value
+(CCScene *) scene {
CCScene *scene = [CCScene node];
GameScene *layer = [GameScene node];
[scene addChild:layer];
return self;
}
I'm still confused of why id is not working cause it can be every type , even CCScene type.
Am uncertain what you are trying to accomplish here, but in your +(id) scene creator, you should not be returning self.
Try [scene autorelease]; (if thats what you really want).
Related
I'm trying to test my service and an some point I received an error when I did the assertEquals
This is my test
#Test
public void createNewCommentCreatesNewDTOIfNoDTOExists() {
CommentDTO commentDTO = mock(CommentDTO.class);
MergedScopeKey mergedScopeKey = mock(MergedScopeKey.class);
//set merged scope key
sut.setInput(mergedScopeKey);
String commentText = "commentText";
//define behaviour
when(commentApplicationService.createCommentDTO(mergedScopeKey, commentText)).thenReturn(commentDTO);
sut.createNewComment(commentText);
//test the functionality
assertNotNull(commentDTO);
assertEquals(commentText, commentDTO.getCommentText());
//test the behavior
verify(commentApplicationService).createCommentDTO(mergedScopeKey, commentText);
}
And this is my method that I wanted to test:
protected void createNewComment(String commentText) {
CommentDTO commentDTO = commentApplicationService.getDTOComment(mergedScopeKey);
if (commentDTO == null) {
commentApplicationService.createCommentDTO(mergedScopeKey, commentText);
} else {
updateComment(commentDTO, commentText);
}
}
Do you have any ideas what I do wrong ?
You define behaviour:
when(commentApplicationService.createCommentDTO(mergedScopeKey, commentText)).thenReturn(commentDTO);
But in your test you call:
CommentDTO commentDTO = commentApplicationService.getDTOComment(mergedScopeKey);
This is a different method, you receive null here.
Even if you fix this, you call updateComment. It is highly unlikely that your production code sets expectations on the passed in mock, thus you will always receive null from commentDto.getCommentText()
Consider using a real class instead of a mock for DTO classes.
I have been trying to assign class's member function to a std::function but it throws compile time error Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)
Below is the header file of that class :
class TypeAnalysis {
public:
bool AnalysisHelper(std::string filePath);
void createTypeTable(std::string dir, std::string pattern, size_t nThread = 3);
std::vector<std::string> getFiles(std::string dir, std::vector<std::string> patterns);
private:
};
And below is createtypeTable function where I am assigning AnalysisHelper method to std::fnuction object.
void TypeAnalysis::createTypeTable(std::string dir, std::string pattern, size_t nThread)
{
Threadpool<bool, std::string> tp(10);
DataContext dc;
tp.start();
std::vector<std::string> patterns = SH::split(pattern);
std::vector<std::string> files = getFiles(dir, patterns);
std::function<bool(std::string)> w = &TypeAnalysis::AnalysisHelper; //I think issue is here
try {
if (files.size() > 0) {
for (size_t i = 0; i < files.size(); i++) {
WorkItem<bool, std::string> *wi1 = new WorkItem<bool, std::string>(&w, &files[i]);
tp.doWork(wi1);
}
}
}
catch (std::exception ex) {
std::cout << ex.what();
return;
}
tp.doWork(nullptr);
tp.wait();
DataContext::getContextThreadPool().doWork(nullptr);
}
When I tried to do the same without any class definition (AnalysisHelper was Glabal function and main had same body as createTypeTable), it worked fine.
Any idea whats wrong?
Yes, the problem is in this line:
std::function<bool(std::string)> w = &TypeAnalysis::AnalysisHelper;
I can not see this as correct operation. You are referencing a member function of a class. This member function may need to edit or read one of the member variables of that class. It is not a free function. It is part of the class which should be called for a specific object. So it is normal that C++ denied that.
Edit:
I found this https://isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr
How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc?
Don’t.
Because a member function is meaningless without an object to invoke it on,
you can’t do this directly (if The X Window System was rewritten in
C++, it would probably pass references to objects around, not just
pointers to functions; naturally the objects would embody the required
function and probably a whole lot more)
Issue was I was not binding the method to any object. Changing
std::function<bool(std::string)> w = &TypeAnalysis::AnalysisHelper;
to
std::function<bool(std::string)> w = [=](std::string file) { return this->AnalysisHelper(file); };
worked.
Helo,
I use WM for my developpement. WM create this code in 'WDObjectiveC.mm':
#include <stdint.h>
#import <UIKit/UIKit.h>
#import <Exe/www/lib/wdHTML.h>
void activeDelegate(void*);
bool RetourObjC(NSString*,NSString*);
void activeDelegate(void*nHandleChampWM) {
UIView*ChampWM= (UIView*)nHandleChampWM;
UIWebView*myView= (UIWebView*)[ChampWM subviews][0];
[myView setDelegate: [wdHTML new]]
NSLog(#"activé");
RetourObjC(#"JS",#"test");
}
...
bool RetourObjC(NSString* p0,NSString* p1){
IExternalCall* call = AllocExternalCall(0x136a26740038d376ll,2);
call->Push(0,&p0,16);
call->Push(1,&p1,16);
bool r = *(bool*)call->Call();
call->Release();
return r;
}
...
'RetourObjC' is a function that i have created in WM, this function execute a code in WLanguage.
When i call RetourObjC here, there isn't problem.
But i have created a class :
#interface wdHTML : UIWebView <UIWebViewDelegate> { }
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType ;
#end
#implementation wdHTML
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(#"Enter");
NSURL *URL = [request URL];
RetourObjC(#"JS",#"test");
return NO;
}
return YES;
}
#end
When i called the function RetourObjC in this class, i have one error : Use of undeclared identifier 'RetourObjC'.
SOS please, i don't find any solution on google (i'm new in the developpement for IOS)
The problem is that your function isn't visible from the wdHTML class. Move the prototype to a header file:
// WDObjectiveC.h
bool RetourObjC(NSString*,NSString*);
and then #import that file in your class:
// wdHTML.m
#import "WDObjectiveC.h"
I am using Xcode 4.3 and I have a label in one view controller that I want to update the text in a text field in another view controller. How should I do this?
You should put the text in your model class (assuming that you have one; if you don't, you need to create it). When the end-user edits the text field, your code should change the string in your model; when the label displays, you should read its text from the model. The easiest way of sharing your model among multiple classes is to define a singleton.
Header:
#interface Model : NSObject
#property (NSString*) labelText;
+(Model*)instance;
#end
Implementation:
#implementation Model
#synthesize labelText;
+(Model*)instance{
static Model *inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = [[Model alloc] init];
});
return inst;
}
-(id)init {
if (self = [super init]) {
labelText = #"Initial Text";
}
return self;
}
#end
Using the model:
// Setting the field in the model, presumably in textFieldDidEndEditing:
// of your UITextField delegate
[Model instance].labelText = textField.text;
// Getting the field from the model, presumably in viewWillAppear
myLabel.text = [Model instance].labelText;
I'm sure I'm missing something in a small iPhone program I'm trying to write, but the code is simple and it compiles without any errors and so I fails to see where the error is.
I've set up a NSMutableDictionary to store students' attributes, each with a unique key. In the header file, I declare the NSMutableDictonary studentStore:
#interface School : NSObject
{
#private
NSMutableDictionary* studentStore;
}
#property (nonatomic, retain) NSMutableDictionary *studentStore;
And of course in the implementation file:
#implementation School
#synthesize studentStore;
And I want to add an object into the dictionary:
- (BOOL)addStudent:(Student *)newStudent
{
NSLog(#"adding new student");
[studentStore setObject:newStudent forKey:newStudent.adminNo];
return YES;
}
class Student has the attributes:
#interface Student : NSObject
{
#private
NSString* name; //attributes
NSString* gender;
int age;
NSString* adminNo;
}
where newStudent has the values:
Student *newStudent = [[Student alloc] initWithName:#"jane" gender:#"female" age:16 adminNo:#"123"];
But when I look up the dictionary:
- (void)printStudents
{
Student *student;
for (NSString* key in studentStore)
{
student = [studentStore objectForKey:key];
NSLog(#" Admin No: %#", student.adminNo);
NSLog(#" Name: %#", student.name);
NSLog(#"Gender: %#", student.gender);
}
NSLog(#"printStudents failed");
}
It fails to print the values in the table. Instead, it prints the line "printStudents failed".
I guess this's quite basic, but since I'm new to iOS programming I'm a bit stumped. Any help will be appreciated. Thanks.
Your studentStore instance variable is a pointer to an NSMutableDictionary. By default, it points to nil, meaning it doesn't point to any object. You need to set it to point to an instance of NSMutableDictionary.
- (BOOL)addStudent:(Student *)newStudent
{
NSLog(#"adding new student");
if (studentStore == nil) {
studentStore = [[NSMutableDictionary alloc] init];
}
[studentStore setObject:newStudent forKey:newStudent.adminNo];
return YES;
}