Why Xcode doesn't recognize a class I created? - ios

I am new to programming and is in the process of learning objective-c from Big Nerd Ranch. Using x-code, I am trying to create a class. Upon creating the class, X-Code is not recognizing it in main file. I created a new project, then created a new file ensuring the correct target was selected. When I try to type triangle, it says "Use of undeclared identifier." What am I doing wrong? Please help.
This is my header file.
#import <Foundation/Foundation.h>
#interface Triangle : NSObject
{
float lengthSideA;
float lengthSideB;
float lengthSideC;
}
#property float lengthSideA;
#property float lengthSideB;
#property float lengthSideC;
-(float) area;
-(float) perimeter;
-(float) hypothenuse;
#end
And this is my implementation file.
#import "Triangle.h"
#implementation Triangle
#synthesize lengthSideA, lengthSideC, lengthSideB;
-(float) perimeter
{
float a = [self lengthSideA];
float b = [self lengthSideB];
float c = [self lengthSideC];
return a + b + c;
}
-(float) area
{
float a = [self lengthSideA];
float b = [self lengthSideB];
return b * a / 2;
}
-(float) hypothenuse
{
float a = [self lengthSideA];
float b = [self lengthSideB];
return sqrt(a * a + b * b);
}
#end

Add #import "Triangle.h" in the class where you are using your Triangle class. You have to import any external class before you can use it.

First of all check if the file is imported. If it is imported, go to build settings and check if the header of the file is added to the compile sources. Also check if the file is added to the main bundle properly. I hope you selected the tick option which says add the reference of the file while adding the file. Hope it helps :)

Related

Using Objective-c classes in Swift Classes

I have forced to use some classes of objective-c language in swift recently, I have searched and found this Apple doc:
To import Objective-C code into Swift from the same target
1) In your Objective-C bridging header file, import every Objective-C
header you want to expose to Swift. For example:
#import "XYZCustomCell.h"
#import "XYZCustomView.h"
#import "XYZCustomViewController.h"
2) In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header
build setting under has a path to the bridging header file. The path
should be relative to your project, similar to the way your Info.plist
path is specified in Build Settings. In most cases, you should not
need to modify this setting.
Any public Objective-C headers listed in
this bridging header file will be visible to Swift. The Objective-C
functionality will be available in any Swift file within that target
automatically, without any import statements. Use your custom
Objective-C code with the same Swift syntax you use with system
classes.
I followed steps and added initial code to my swift class:
let percentageView: FSPercentageView = FSPercentageView(frame: CGRectMake(15, 65, 300, 300))
But I got an error with message: Used of undeclared type 'FSPercentageView'.
I have searched this error related using objective-c in swift but I did not find any useful answer.
I checked the bridge header file path and It seems to be fine.
I hope your suggestion fix my problem.
UPDATE:
My Bridging-Header.h
#import "FSPercentageView.h"
My Class of FSPercentageView.h :
#import <Foundation/Foundation.h>
#import "MCCore.h"
#class FCPercentageView;
typedef enum {
FSPercentageViewTextStyleAutomaticShowsPercentage,
FSPercentageViewTextStyleUserDefined
} FSPercentageViewTextStyle;
#interface FSPercentageView : MCNewCustomLayeredView
/*
Sets the percentage of the view.
*/
#property (nonatomic) CGFloat percentage;
/*
Sets the percentage of the view.
*/
#property (nonatomic) CGFloat initialPercentage;
/*
Defines the border percentage for both filled and unfilled portions.
*/
#property (nonatomic) CGFloat borderPercentage;
/*
The label of the text in the center of the doughnut.
*/
#property (nonatomic, retain) UILabel *textLabel;
/*
The color for the filled portion of the doughnut.
*/
#property (nonatomic, retain) UIColor *fillColor;
/*
The color for the unfilled portion of the doughnut.
*/
#property (nonatomic, retain) UIColor *unfillColor;
}
and the initial segment of FSPercentageView.m:
#import "FSPercentageView.h"
#import "FSNewCustomLayeredView+FSCustomLayeredViewSubclass.h"
typedef enum {
FSPercentageViewStateNormal,
FSPercentageViewStatePushed
} FSPercentageViewTouchState;
#interface FSPercentageView()
#property (nonatomic, retain) UIView *centerView;
#property (nonatomic) FSPercentageViewTouchState touchState;
#end
#implementation FSPercentageView
- (void)setDefaults
{
[super setDefaults];
self.linePercentage = 0.15;
self.borderPercentage = 0;
self.showTextLabel = YES;
self.animationDuration = 0.5;
self.unfillColor = [MCUtil iOS7DefaultGrayColorForBackground];
self.borderColorForFilledArc = [UIColor blackColor];
self.borderColorForUnfilledArc = [UIColor clearColor];
self.borderPercentageForFilledArc = -1;
self.borderPercentageForUnfilledArc = -1;
self.adjustsFontSizeAutomatically = YES;
self.roundedImageOverlapPercentage = 0;
self.touchState = FSPercentageViewStateNormal;
self.gradientColor1 = [MCUtil iOS7DefaultBlueColor];
self.gradientColor2 = [MCUtil iOS7DefaultGrayColorForBackground];
_percentage = 0.0;
_initialPercentage = 0.0;
_centerView = [[UIView alloc] init];
_textLabel = [[UILabel alloc] init];
}
- (void)willDrawSublayers
{
if (!self.fillColor) {
self.fillColor = self.tintColor;
}
}
- (Class)classForSublayers {
return [MCSliceLayer class];
}
and the swift code:
let percentageView: FSPercentageView = FSPercentageView(frame: CGRectMake(15, 35, 289, 311))
Shows the error in above line.
I hope your suggestion fix my problem.
But you have not given any information, so no suggestion is possible. What Apple says is true; I assure you that this feature does work. The problem can only be that you have not followed the directions that you yourself have quoted.
I found the problem which my code has.
I have two targets in my app and also I am using Test Unit.
Because of adding my swift code in all mentioned targets, the project was needed to have 3 bridging class related to each target and also the headers must be imported in all of them. Actually the red message was about unit test bridging file.
Right now, I removed my swift class from Test Unit and The code is compiling and running without any problem.

Howto achieve animation like store house in ios?

Hi all i have got to implement storehouse like animation in my project. I found a third party library but sorry it is in swift. Please help me with objective c code
Refference link for animation is
Animation Store house
StoreTableView.h
#import <UIKit/UIKit.h>
#protocol StoreHouseTableViewTransform
#property (nonatomic) CGFloat scaleValue;
-(void)transFormForCell:(CGFloat)scaleFactor;
#end
#interface StoreTableView : UITableView
#end
StoreTableView.m
#import "StoreTableView.h"
#implementation StoreTableView
-(void)layoutSubviews{
[super layoutSubviews];
[self transformTable];
}
- (void )transformTable{
for (NSIndexPath *indexpath in self.indexPathsForVisibleRows) {
UITableViewCell *cell = [self cellForRowAtIndexPath:indexpath];
if ([cell conformsToProtocol:#protocol(StoreHouseTableViewTransform)]) {
UITableViewCell <StoreHouseTableViewTransform> *vc = (UITableViewCell<StoreHouseTableViewTransform> *)cell;
CGFloat distCenter=[self computeDistance:indexpath];
[vc transFormForCell:[self computeScale:distCenter withMinScale:vc.scaleValue]];
}
}
}
-(CGFloat )computeDistance:(NSIndexPath *)indexpath{
CGRect rect=[self rectForRowAtIndexPath:indexpath];
CGFloat cellCenter=rect.origin.y + rect.size.height/2;
CGFloat cellContentCenter=self.contentOffset.y + self.bounds.size.height/2;
return fabs(cellCenter - cellContentCenter);
}
-(CGFloat )computeScale:(CGFloat)distanceOfCenter withMinScale:(CGFloat)scaleFactor{
return (1.0-scaleFactor) * distanceOfCenter / self.bounds.size.height;
}
#end
StoreTableViewCell.h
#import <UIKit/UIKit.h>
#import "StoreTableView.h"
#interface StoreTableViewCell : UITableViewCell<StoreHouseTableViewTransform>{
CGFloat minimumScale;
}
#property (weak, nonatomic) IBOutlet UIView *backView;
#property (weak, nonatomic) IBOutlet UIImageView *scaleImage;
#end
StoreTableViewCell.m
#import "StoreTableViewCell.h"
#implementation StoreTableViewCell
#synthesize scaleValue;
-(void )prepareForReuse{
[super prepareForReuse ];
minimumScale = 0.85;
self.backView.transform = CGAffineTransformMakeScale(minimumScale, minimumScale);
}
#pragma mark delegate
-(void)transFormForCell:(CGFloat)scaleFactor{
self.backView.transform = CGAffineTransformMakeScale(1.0 - scaleFactor, 1.0 - scaleFactor);
}
#end
This is objective-C implementation of StoreHouse Animation
Subclass your TableView with StoreTableView and Cell with StoreTableViewCell.
I think so this will help you
play with scaleFactor
I spent about 4 hours trying to enable Swift in my Xcode Objc-based project. My "myproject-Swift.h" file was created successfully, but my Xcode didn't see my Swift-classes. So I decided to create a new Xcode Objc-based project and finally I found the right answer! Hope this post will help someone :-)
Step by step swift integration for Xcode Objc-based project:
1.Create new *.swift file (in Xcode) or add it by using Finder
2.Add swift bridging empty header if Xcode have not done this before (see 4 below)
3.Implement your Swift class by using #objc attribute:
import UIKit
#objc class Hello : NSObject {
func sayHello () {
println("Hi there!")
}
}
4.Open Build Settings and check this parameters:
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h
5. Import header (which is auto generated by Xcode) in your *.m file
#import "myproject-Swift.h"
6. Clean and rebuild your Xcode project
Profit
They have open sourced their animation engine. Built on top of CADisplayLink, they recommend this mainly if you're looking to do physics based animations that interact with gestures.
https://github.com/storehouse/Advance

Using SPUserResizableView in Swift

I'm trying to use the SPUserResizableView created by Spoletto in github (https://github.com/spoletto/SPUserResizableView).
I imported the .h and .m files, created a bridge and added the delegates to my controller.
I think problem is that problem is that the library is very old and that's why I get many errors in the .h and .m files so I can't use the library.
Errors - http://postimg.org/gallery/3bq2ldo0m/
Can you help me setup the library in swift?
I also got the same error, Just add these two in SPUserResizableView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
Your problem will be resolved.
You need to set up an Objective-C bridging header and then import it in there. To do this:
Drag and drop the .h and .m SPUserResizableView files into your project.
When Xcode asks if you want to create a bridging header, select 'Yes'.
Type in the new file called 'YOUR-PROJECT-NAME-bridging-header.h' this code:
#import SPUserResizableView.h
You should now be able to use it in Swift!
I got this working with Swift 4 by moving the variable declarations from the interface to the implementation, and removing the release and dealloc code.
SPUserResizableView.h changed to
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
typedef struct SPUserResizableViewAnchorPoint {
CGFloat adjustsX;
CGFloat adjustsY;
CGFloat adjustsH;
CGFloat adjustsW;
} SPUserResizableViewAnchorPoint;
#protocol SPUserResizableViewDelegate;
#class SPGripViewBorderView;
#interface SPUserResizableView : UIView {
}
//...
//(identical from this point on)
SPUserResizableView.m changed to
//...
//(identical up to)
//CGColorSpaceRelease(baseSpace), baseSpace = NULL;
CGColorSpaceRelease(baseSpace);
baseSpace = NULL;
#implementation SPUserResizableView
SPGripViewBorderView *borderView;
UIView *contentView;
CGPoint touchStart;
CGFloat minWidth;
CGFloat minHeight;
// Used to determine which components of the bounds we'll be modifying, based upon where the user's touch started.
SPUserResizableViewAnchorPoint anchorPoint;
id <SPUserResizableViewDelegate> delegate;
//#synthesize contentView, minWidth, minHeight, preventsPositionOutsideSuperview, delegate;
//...
//(this part identical)
- (void)dealloc {
[contentView removeFromSuperview];
}
#end

why can't I put a conditional statement in file to set global variable values?

I would like to define a number of variables that I will use in view controllers to set font size depending on whether the user is on an iphone or an ipad. So far I can define, say, a global float no problem. However, I am not able to include any conditional statements in my file. This works fine:
.h file:
#import <Foundation/Foundation.h>
#interface GlobalDeclarations : NSObject
extern float MyGlobalVariable;
#end
.m file:
#import "GlobalDeclarations.h"
#implementation GlobalDeclarations
float MyGlobalVariable = 0.0f;
#end
But I cannot modify my .m file to include any if statement at all - I always get the same error:
"expected idenitifer or ')'
For example, this version of the .m file produces the above error:
#import "GlobalDeclarations.h"
#implementation GlobalDeclarations
float MyGlobalVariable = 0.0f;
if(MyGlobalVariable > 2.0f)
{
NSLog(#"inside if statement");
}
#end
I get the error even if I just write something like if(TRUE)...
How can I conditionally define globals in an attachment like this? What is wrong with the if-statement? Thanks for any suggestions.
For variables you can only have declarations and limited initialisation expressions as the file level. If you need more complex initialisation you must put the code in a method, and Objective-C supports the method + initialize for doing class-wide setup.
Rewrite your code as:
#import "GlobalDeclarations.h"
#implementation GlobalDeclarations
float MyGlobalVariable = 0.0f;
+ initialize
{
if(MyGlobalVariable < 2.0f)
{
NSLog(#"inside if statement");
}
}
#end
The + initialize method will get called before the first use of the class.
Note: for more obscure situations there is also a + load method which gets called even earlier than + initialize, and there are attributes you can use to mark any function to be called in a similar way.

Grabbing iPhone screenWidth and Height for reusability

I created a category like so:
#import "UIViewController+Dimensions.h"
#implementation UIView (Dimensions)
- (double) screenWidth2
{
return self.frame.size.width;
}
- (double) screenHeight2
{
return self.frame.size.height;
}
#end
But when I try to call it in my view controller after importing it, I get:
"Use of undeclared identifier screenWidth2;
Is there a better way to grab the screenWidth in one place then every view controller I'm in?
I then did this in my view controller:
_registerButton.frame = CGRectMake(0.0, 0.0, [self screenWidth2] / 2.0 - 1.0, 60.0);
but the app compiles runs and crashes. And the category's .h file is:
#import <UIKit/UIKit.h>
#interface UIViewController (Dimensions)
- (double) screenWidth2;
- (double) screenHeight2;
#end
You need to add
#import "UIViewController+Dimensions.h"
In the code using the category (the file where you call [uiViewControllerInstance screenWidth2])
Have you checked that those variables are declared in the .h? So, they are public.
Also, to use the category, add this:
#import "UIViewController+Dimensions.h"
In the class that have to use the category.

Resources