Using NSlog with cross platform style - ios

I am creating an iphone test using c++,and I want to use nslog( the only way to show debug information?) whenever I want to include nslog function my cpp has to change to mm right? is it the only way to do it? because I don't want to change back to cpp when I move to other platform.
thanks for help

Since NSLog only exists on Objective-C platforms, why not create your own LogMessage() method taking standard C++ arguments in a single .mm file that calls NSLog and call that from C++ files named as usual. That way you'll not need to rename every single file that needs to log to .mm
On other platforms, just include another implementation of LogMessage() since there's no NSLog to call there anyway.

Related

How can I modify the contents of an public m file in a dynamic library?

I made a framework, The files I chose to make public were .h and .m files. I found that if I modify the contents of the .m file directly, it won't take effect. So what should I do to take effect?
Maybe I'm misunderstanding your question, but in the abscence of other answers let's see if I can help:
I made a framework
So you wrote some text into some files; then you used a tool, probably Xcode, to invoke the compiler, which interpreted that text as Objective-C and produced machine code in another file, and then constructed a framework bundle for you.
The files I chose to make public were .h and .m files. I found that if I modify the contents of the .m file directly, it won't take effect.
So now you edit your text file, and what do you expect to happen - not sure. Do you expect the framework code to change? If so aren't you missing a step compared to the above?
So what should I do to take effect?
Well that depends on what your goal is here. If you want your users to be able to customise your framework in some way then you need to design a method to do that using whatever tools you can when iOS is your target (Apple has rules).
This answer isn't much, but hope it helps.

Using C++ library in iOS application

I've a task in which I need to use a .a c++ library (don't have any idea what is the code in that library) with a header file having some methods declared in it. Now what I need to do is to call these methods from my objective c class. for this I've done following things:
changed the extension of my view controller class from .m to .mm
set my 'other C++ flags' in build settings to objective-c++
all other steps which were suggested at stack overflow and any where else.still I'm not able to use that library. An example will give you all the better understanding on my problem:
suppose We've a library mylibrary.a,We also have a header file named myHeaderFile.h. Now we need to call a method named 'int Login(unsigned long *LoginInfo)' which is declared in myHeaderFile.h header file. What should be done? The screen shot is the crash report when I try to call C++ method
What can be mistake here,please suggest. A quick help will be appreciated...
Attach to the app with any debugger or turn on Error Reporting to see what exactly crashes.
UPD: Apple's LLDB tutorial. You need these steps:
Specifying the Program to Debug
Launching the Program with LLDB (r, then Enter)
Finally, when it crashes, type bt, then Enter.

Can I detect what files are in a project at compile time?

We have a suite of apps which share a set of common screens. Some screens have use more functionality in some apps than others. For example, in one app our issue screen supports tags, but in the others it doesn't. In one, our regions screen supports workflows, but in the others it doesn't.
We're using core data, and the different apps have different data models; If an entity isn't used by an app's data model, the corresponding class doesn't get built into that app.
The result of this is that some of our controllers contain references to classes that may or may not exist when the controller is used in a particular app. At the moment, we get around this using "magic" #defines in our apps' PCH files; so some code in our Issue controller is wrapped in #ifdef SGB_ISSUES_HAVE_TAGS and some code in our Regions controller is wrapped in #ifdef SGB_REGIONS_ARE_FILTERED_BY_WORKFLOW.
What I'd like is to do this instead by detecting whether a file has been included in the project. So at the top of our Issue controller I'd have something like this:
#if exists_in_project("SGBIssueTag.h")
#import "SGBIssueTag.h"
#endif
and then I could put #ifdef SGB_ISSUES_HAVE_TAGS into SGBIssueTag.h; any app we built that used the issue controller would get the issue tag functionality as soon as we included the IssueTag entity, and we wouldn't have to mess around with the PCH file.
Is anything like this possible?
This issue has bitten a lot of people - the issue of header files being "global" to a project.
My suggestion is to try and force errors during compilation. You can add in a PCH or file included there defines for each App - lets say you have app A, B, and C. So during any one build, only one of these is active.
Then, in every .m file (and possibly some .h files), you will conditionally include headers:
...
#if defined(A) || defined(B)
#include "SomeInterfaceFile.h"
#endif
...
Once you start building A, you will get compilation errors in files where the source code refers to some object that is defined in a out-of-scope header file. That code you will also have to wrap in a similar if statement.
This will work perfectly, with the downside that your code gets cluttered with these conditional statements.

Xcode doesn't hit breakpoint when debugging on device

I'm trying to debug a certain method, but my breakpoints in that method never get hit when I'm debugging on a device, but they do get hit when I am debugging with the iOS simulator. The method is part of a library that my app calls, and I feel like that may be a factor. I developed both the app and the library, and I have both the app and the library open in the same Workspace when I'm debugging.
I put one breakpoint in on the line in my app's code that calls the library method, then a second breakpoint on the first line of the library. When I test with the simulator, both are hit. When I test on a device, only the first one is hit, and the second one gets skipped over (the code for the library method still appears to run).
Is there anything special I need to do to be able to step into a library call when I'm debugging on a device that I don't need to do when debugging on the simulator? Or is it impossible to debug inside a library?
I'm sorry if this is a little unclear - I'm trying to describe what I'm doing as well as I can, but it's pretty strange behavior that I haven't seen before, so it's a little hard to describe. If you need anymore information or anything is unclear, please let me know.
EDIT 1: I noticed something that's a little different, not sure if it's connected or not: I don't have a .h file containing the methods in my library. Actually, there is only 1 method in it (which I'll callmyMethod) that will be called from outside the library (then that method calls the other methods in the library as it needs them). Furthermore, I'm using the TARGET_IPHONE_SIMULATOR pre-processor trigger to make the call in a slightly different manner depending on if it's running on a device or on the simulator. To make the call into the library, on a button click method in my app I just have
#if TARGET_IPHONE_SIMULATOR
extern void myMethod();
myMethod();
#else
extern void myMethod(const char diretory[], int directoryLength);
const char *dir = [[documents path] UTF8String];
myMethod(dir, [[documents path] length]);
#endif
I have matching #if blocks in the method in my library to declare it as needed, and to use dir when it is passed in. This seems to execute just fine. I can tell the code in my library is being called and doing something, it's just not doing what I want it to. Thus the attempts to put in a breakpoint, and the reason for this whole question.
EDIT 2: My library actually contains plain C code, not Objective-C. I put some printf lines into the library code, and its not being displayed. I'm curious if somehow my app is calling a previous version of my library, instead of calling the one that's open in my workspace. That would be consistent with the "it's just not doing what I want it to" part of my last edit. Would it be possible that the device is trying to run an old version of the library it has saved somehow? I thought that completely removing the app from the device before installing the new version of my app would prevent that, but perhaps I'm mistaken?

Loading STL into an Objective-C project and compiling

A Stack Overflow post about data structures said you can use STL in Objective-C and iOS development. The link he gave has the link to STL download as http://www.sgi.com/tech/stl/
It is nothing but header files and I'm not sure whether I need any other files. But let's say that works. I thought I could just put the files in a folder in my project folder and add them all to the project I have, so far a simple chat client. But would that cause them all to load in my file view? Is there a right way to do this?
all you need to do is to rename the file to *.mm. this tell the compiler it is Objective-C++ code. you can use both Objective-C object and C++ object at same time.
then if you want to use vector, just #include <vector> and use it like in normal C++ code.

Resources