At the start this is what I input:
property (strong, nonatomic) IBOutlet UITextField *userName;
This is the code for outputting the data:
void printstructure_1 (void){
printf("Name:");
printf("%s," , userName);
}
and I want it that such that when I press a button with this code:
-(IBAction)Next:(id)sender {
int printstructure_1 ();
}
but after I tested it on the simulator, the data I keyed into the UITextField do not register and display a blank.
What you will need to do is pass the contents of the UITextField to the C function, so add a parameter to the function:
cfunctions.h:
extern void printstructure_1(const char *userName);
cfunctions.c:
void printstructure_1(const char *userName){
printf("Name: %s", userName);
}
And then get the current text contents of the UITextField in the button action method, converting it to a C-String:
#import "cfunctions.h"
-(IBAction)Next:(id)sender {
printstructure_1([self.userName.text UTF8String]);
}
Related
Why I cant use property on void function under header #interface ?
#property (nonatomic) void testjj;
-in m file:
-(void)testjj{
NSLog(#"testjtestj");
}
Cannot synthesize property 'testjj' with incomplete type 'void'
The comments deal with declaring a void variable as an ivar. The comments also elaborate a bit on the difference between void and void pointer. This is important.
It seems you want to have an ivar that is, in fact, a void function. That is of course possible, if you make use of void pointer and not void, as shown below.
// A few functions for later
void f1( int a, int b )
{
NSLog(#"f1");
}
void f2( int a, int b )
{
NSLog(#"f2");
}
void F1( void )
{
NSLog(#"F1");
}
void F2( void )
{
NSLog(#"F2");
}
// Typedef makes life easy
typedef void ( func )( int a, int b );
typedef void ( Func )( void );
// Some class
#interface MyClass : NSObject
// Defining ivars ...
// Typedef is easier
#property (nonatomic) func * ma;
#property (nonatomic) Func * mb;
// Direct is also possible
#property (nonatomic) void ( * na )( int, int );
#property (nonatomic) void ( * nb )( void );
#end
#implementation MyClass
// Assigning the ivars
- ( void ) setIt
{
self.ma = f1;
self.mb = F1;
self.na = f2;
self.nb = F2;
}
// 'Using' the ivars
- ( void ) doIt
{
self.ma ( 1, 2 );
self.mb ();
self.na ( 1, 2 );
self.nb ();
}
// Have some fun
- ( void ) swapIt
{
func * t1 = self.ma;
Func * t2 = self.mb;
self.ma = self.na;
self.mb = self.nb;
self.na = t1;
self.nb = t2;
}
#end
in .h
- (void)testjj;
in .m
self.testjj;
That is OK.
Hi I am trying to learn Opps concept in Objective C but I know PHP so I took a program in which for public, private and protected mentioned bellow.
<?php
//Public properties and method can be inherited and can be accessed outside the class.
//private properties and method can not be inherited and can not be accessed outside the class.
//protected properties and method can be inherited but can not be accessed outside the class.
class one
{
var $a=20;
private $b=30;
protected $c=40;
}
class two extends one
{
function disp()
{
print $this->c;
echo "<br>";
}
}
$obj2=new two;
$obj2->disp(); //Inheritance
echo"<br>";
$obj1=new one;
print $obj1->c; //Outside the class
?>
So this I am trying to convert in Objective c code mentioned bellow.
#import <Foundation/Foundation.h>
#interface one : NSObject
{
#private int a;
#public int b;
#protected int c;
}
#property int a;
#property int b;
#property int c;
#end
#implementation one
#synthesize a,b,c;
int a=10;
int b=20;
int c=30;
#end
#interface two : one
-(void)setlocation;
#end
#implementation two
-(void)setlocation;
{
// NSLog(#"%d",a);
NSLog(#"%d",b);
// NSLog(#"%d",c);
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
// insert code here...
two *newtwo;
newtwo =[[two alloc]init];
//calling function
[newtwo setlocation];
}
return 0;
}
When I run the above code I am getting
2015-11-03 23:20:16.877 Access Specifier[3562:303] 0
Can some one resolve my problem.
This type of question has been asked before and there's a good explanation in the accepted answer for Private ivar in #interface or #implementation
In general I would recommend you avoid instance variables and use #property instead. Properties have the benefit of read-only/write controls, and free synthesized setters and getters (which if you're learning OOP concepts is a critical concept you should employ).
Properties are declared in the #interface part of an Obj-C file. For access control (according to the link) you have no public/private/protected keywords. All Obj-C methods (and by extension, properties) are public if they're defined in the .h file. If you want them "private" you define them in the the .m file using a class category:
//MyClass.m
#interface MyClass ()
#property(nonatomic, retain) NSString* myString;
#end
#implementation MyClass
#end
I'm having Objective-C project and i'd like to try Swift in it. I was able to configure it to use Swift classes. Anyway i can't see Swift object properties while debugging:
I've even overriden description property to print all the variables:
import Foundation
import ObjectMapper
#objc
public class Mcu : NSObject, Mappable {
var name : String?
var arch : String?
var macro : String?
var libraryName : String?
required public init?(_ map: Map) {
}
// Mappable
public func mapping(map: Map) {
name <- map["name"]
arch <- map["arch"]
macro <- map["macro"]
libraryName <- map["libraryName"]
}
override public var description : String {
return "name=\(name), arch=\(arch), macro=\(macro), libraryName=\(libraryName)"
}
}
If i print object in log (NSLog([mcus[0] description])) i
m getting correct string:
2015-11-01 10:27:19.262 Project[1447:261056] name=Optional("avr2"), arch=Optional("ARCH_AVR2"), macro=nil, libraryName=Optional("\"s8515\"")
It's not the solution to convert to Swift all the project as it's pretty large.. What can i do to provide convenient debugging for both Obj-C and Swift in Obj-C project?
PS. Xcode 7.1
PPS. For Swift class Mcu Xcode generates according Obj-c header and it looks correct (but there is difference for debugger):
SWIFT_CLASS("_TtC17ProjectModule3Mcu")
#interface Mcu : NSObject
#property (nonatomic, copy) NSString * __nullable name;
#property (nonatomic, copy) NSString * __nullable arch;
#property (nonatomic, copy) NSString * __nullable macro;
#property (nonatomic, copy) NSString * __nullable libraryName;
#property (nonatomic, readonly, copy) NSString * __nonnull description;
#end
I m new for IOS. I have some source code for OS X and java. I was trying to convert to IOS.
In OS X, I have the following.
struct _NoteData {
int number; /** The Midi note number, used to determine the color */
WhiteNote *whitenote; /** The white note location to draw */
NoteDuration duration; /** The duration of the note */
BOOL leftside; /** Whether to draw note to the left or right of the stem */
int accid; /** Used to create the AccidSymbols for the chord */
};
typedef struct _NoteData NoteData;
#interface ChordSymbol : NSObject <MusicSymbol> {
_NoteData notedata[20];/** The notes to draw */
}
_NoteData is like an array and class here. number, whitenote,duration..are instance variable for _noteData.
I was trying to change struct to objective c class:
#interface _NoteData:NSObject{
#property NSInteger number_color;
#property WhiteNote *whitenote;
#property NoteDuration duration;
#property BOOL leftside;
#property NSInteger accid;
};
#interface ChordSymbol : NSObject <MusicSymbol> {
_NoteData notedata[20];/** The notes to draw */
}
In my .m file, it has
+(BOOL)notesOverlap:(_NoteData*)notedata withStart:(int)start andEnd:(int)end {
for (int i = start; i < end; i++) {
if (!notedata[i].leftside) {
return YES;
}
}
return NO;
}
!notedata[i] throw error expected method to read array element. I understand _NoteData is a class, not an array. What should I change?
In java:
private NoteData[] notedata;
NoteData is a class, and notedata is an array which store NoteData.
Same method in java
private static boolean NotesOverlap(NoteData[] notedata, int start, int end) {
for (int i = start; i < end; i++) {
if (!notedata[i].leftside) {
return true;
}
}
return false;
}
I feel all I need is to declare an array with _NoteData object. How can I do that?
Objective-C is a superset of C, so you can use C struct in Objective-C code. You can keep your code in the first paragraph. You need to move the function declaration in ChordSymbol class's header file.
+(BOOL)notesOverlap:(NoteData*)notedata withStart:(int)start andEnd:(int)end;
In another Objective-C class's implementation file, call the Class function like this.
NoteData y[] = {
{ .leftside = YES },
{ .leftside = YES },
{ .leftside = YES },
{ .leftside = YES }
};
BOOL result = [ChordSymbol notesOverlap:y withStart:0 andEnd:3];
NSLog(#"%d",result);
Edit
You can use NSArray for this purpose. You create an array and populate its data with NoteData objects.
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:20];
NoteData *data1 = [[NoteData alloc] init];
data1.number_color = 1;
[array addObject:data1];
Then you should change (_NoteData*)notedata to (NSArray*)array, and it should work.
I need to get access to an object inside a C function similar to this few code
#interface MixerHostAudio () <UIApplicationDelegate>
#property (readwrite) int *alternativeOutput;
#end
#implementation MyCode
#synthesize alternative
void audioInputAvailable () {
alternative=1;
}
I get this error: " 'Use of undeclared identifier 'alternative' "
Any ideas about how can i solve it ?
You have to make your "MyCode" object available somewhere for your C glue function to pick up. For example, if you have a pointer to your MyCode object...
void audioInputAvailable(MyCode *myCodeObject){
myCodeObject.alternative = 1;
}